Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JVM crash dump -- free objects..

 
Pavan Kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to get the data from a table and put the data for each record in a map and place them in an array list. The problem is that after sometime, the JVM is crashing as due to insufficient memory on the JVM Heap.. The code looks something like below.. I guess I should pull the resultMap initialization outside the loop and reuse it..

List lst = new ArrayList();

for(int i=0; i< 60000; i++){
Map resultMap = new HashMap();
resultMap.put("XXX", YYY);
resultMap.put("ZZZ", PPP);
lst.add(resultMap);
}

Change it to :

List lst = new ArrayList();
Map resultMap = new HashMap();

for(int i=0; i< 60000; i++){
resultMap.put("XXX", YYY);
resultMap.put("ZZZ", PPP);
lst.add(resultMap);
resultMap.removeAll();
}

Can anyone please suggest on this..
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding an object to a list adds a reference to that object. In your second version of the code, you'll have a list with 60,000 references to the same map, and that map will be empty. Not very useful.

In any case, if you're running out of heap space for your large data structure, then you can just use a bigger heap; check out the -XmxNNm command-line switch to the Java executable.
 
Pavan Kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Friedman for the reply..
Does this mean I have wait for the code to exit out of the function and the garbage collector to free all the 60,000 instances of the object before I can resuse them?? Here is one more question which I would like to ask you.. Once I place all the 60,000 records in an array list, I will be using that data to query another table to find any changes and if there changes, I need to update the first table with the changes.. I was calling a function which would perform the update and then close the resultset and statement objects.. Now my question is will it be better to call the function something like 30,000 times to do update or place those 30,000 records in an arraylist and call the function once to do the update 30,000 times.. I appreciate your help..

- Pavan.

Originally posted by Ernest Friedman-Hill:
Adding an object to a list adds a reference to that object. In your second version of the code, you'll have a list with 60,000 references to the same map, and that map will be empty. Not very useful.

In any case, if you're running out of heap space for your large data structure, then you can just use a bigger heap; check out the -XmxNNm command-line switch to the Java executable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic