optimize java code to avoid consuming too much memory

You need to optimise the following code as it will consume more and more memory after startup.

It can only run for several hours on the server and the server will run out of memory for the java heap space.

final int BATCH_SIZE = 3000;

Connection dbConnection = null;

PreparedStatement preparedStatement = null;

String connectionString = [url removed, login to view]("ConnectionString")+"&rewriteBatchedStatements=true";

try {

dbConnection= [url removed, login to view](connectionString);

[url removed, login to view](false);

int sleepCount = 1;

StringBuilder offerString = new StringBuilder();

while (true) {

ArrayList<MessageQueueEntity> queueItems = [url removed, login to view]();

Iterator<MessageQueueEntity> itemIter = [url removed, login to view]();

while ([url removed, login to view]()) {


MessageQueueEntity message = [url removed, login to view]();

OfferEntity offer = processOffer(message);

if(sleepCount % BATCH_SIZE == 1) {

[url removed, login to view]("INSERT INTO offer " +

"(asn,itemCondition,amount,amount_bookepot, " +

"timeofPolling,timeofUpdate,updated) VALUES ");


[url removed, login to view]("('" + [url removed, login to view]() + "', ");

[url removed, login to view]("'" + [url removed, login to view]() + "' , ");

[url removed, login to view]([url removed, login to view]() + ", ");

[url removed, login to view](offer.getAmount_Bookepot() + ", ");

[url removed, login to view]("'" + [url removed, login to view]() + "', ");

[url removed, login to view]("CURRENT_TIMESTAMP" + ",");

[url removed, login to view]("1" + ")");

if(sleepCount % BATCH_SIZE !=0) {

[url removed, login to view](" , ");


[url removed, login to view](" ON DUPLICATE KEY UPDATE amount= VALUES(amount), " +

"amount_bookepot= VALUES(amount_bookepot), " +

"updated = 1;");

preparedStatement = (PreparedStatement) [url removed, login to view]([url removed, login to view]());

[url removed, login to view]();

[url removed, login to view]();

[url removed, login to view](0);

[url removed, login to view](BATCH_SIZE+" offers were inserted into offer table!");



}catch (Exception e){

[url removed, login to view]();


} //inner loop

if ([url removed, login to view]()) break;

}//whole loop

} catch (Exception e) {

[url removed, login to view]([url removed, login to view]());

//[url removed, login to view]();

} finally {


Taidot: Amazon Web Services, Java

Näytä lisää: optimize some java code, memory card matching java code, sample java dynamic memory allocation code, selective repeat source code java, multithreaded ftp server code java, tree source code java, code java code send file via bluetooth, extract code java jar, optimize code vba excel, instant messenger source code java, source code java row, program tree morse code java, tree code java, florist website project code java, hotel management system source code java, restaurant management software development code java, video streaming source code java

Tietoa työnantajasta:
( 5 arvostelua ) carlton, Australia

Projektin tunnus: #13076567

Myönnetty käyttäjälle:


Hello, I've just updated your codes. Please check it and let me know how it works. Please note that you must call close func of prepareStatement after executeUpdate. I think the memory issue is occurred by witho Lisää

$30 AUD 1 päivässä
(24 Arvostelua)

14 freelanceria on tarjonnut keskimäärin %project_bid_stats_avg_sub_26% %project_currencyDetails_sign_sub_27% tähän työhön


One of the option is to increase java heap size. If it doesn't solve the problem then we can optimize the code for you

$50 AUD 2 päivässä
(51 arvostelua)

I am an experienced Java developer. I will optimize software for your. Feel free to contact me for further discussion. Regards, Moeen Ahmed

$50 AUD 3 päivässä
(13 arvostelua)

Amazon Web Services: Lambda, API Gateway, EC2, Load Balancers, RDS, Elastic Beanstalk, Elastic File System, IAM, VPC, Security Groups, Cloud Formation,, S3, CLoudFront, Redshift, Route 53, Cloud Watch, Kinesis, SNS, SQ Lisää

$99 AUD 0 päivässä
(12 arvostelua)

Hello, Let me introduce myself: Over ten years of experience in IT industry, architecture, design, development and testing of multi-tier enterprise applications and services of e-commerce by using Java technologies and Lisää

$107 AUD 2 päivässä
(20 arvostelua)

Hi, I am a software developer with 8+ years of experience in Java/J2EE development. My specialization is in Memory and CPU profiling using tools like Yourkit, JProfiler, and VisualVM I believe I can really help y Lisää

$111 AUD 3 päivässä
(17 arvostelua)

Hello, i found the bad code snippet that make the memory overflow , please contact me Lisää

$66 AUD 3 päivässä
(6 arvostelua)

We are PR Technologies. We have a team of experienced Java developers, who works together and contribute to Develop Quality Products. So, if you show some trust, then we will guaranteed for a Best products. Thanks & Re Lisää

$44 AUD 2 päivässä
(7 arvostelua)

Hello, I've reviewed the code and the problem is easy to identify, I guess you know the cause is the cyclic loop. For the correct optimization it seems that the best way would be to apply a design pattern called Obser Lisää

$190 AUD 4 päivässä
(3 arvostelua)

hi, i geuss this job is mine.I m a computer science engineering student . i worked a lot with java and i made many apps using netbeans and eclipse .I am Oracle Certified Associate Java SE 7. i can accomplish this task Lisää

$50 AUD 0 päivässä
(7 arvostelua)
$155 AUD 3 päivässä
(3 arvostelua)

Hi, I am an IT engineer. Hi have much experience in java I'm quite sure I can find the memory leaks and fix them successfully. I've faced many issues like that in the past, where my memory was just increasing until it Lisää

$100 AUD 3 päivässä
(0 arvostelua)

I would love to do this job.

$111 AUD 3 päivässä
(0 arvostelua)

A proposal has not yet been provided

$133 AUD 3 päivässä
(0 arvostelua)