Results 1 to 8 of 8
- 03-15-2011, 02:40 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Java Heap Space : ArrayList<Integer>
Dear All,
I am expected to process 50,000,000 records from a database. I take 100,000 records at a time store it in ArrayList<Integer> at a time in order to process. I use 64 bit windows machine and I have increased the heap space as much as possible
java -Xms500m -Xmx1500m Codename
I choose ArrayList instead of Hashmap since it not a specific search for records but rather looping through the whole list. Few research study in google advised me to choose arraylist over hashmap.
But I still get java.lang.OutofMemoryError: Java heap space. What is so weird is I get this error after processing every 5,000,000 records..which I dont understand at all...
But anyway the question is using ArrayList<Long> over ArrayList<Integer> will it improve the performance. As far as I know values stored in the ArrayList when it is a number and needs more bit space we have to use Long instead of Integer. Is my understanding right? Please correct me if I wrong.. And if you have suggestion for my heap space problem I would really appreciate..Last edited by Ms.Ranjan; 03-15-2011 at 02:44 PM.
- 03-15-2011, 03:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You are (somehow, somewhere) holding onto those references, and more.
Not knowing what it is your system is supposed to do, or what it is doing, I can't say more than that.
As for Long or Integer, that depends on whether you need to store longs or integers. Performance is irrelevant at this point.
- 03-15-2011, 03:34 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Thank you tolls. Here is the code snippet
making the list =null and intializing the list again will not help loose the reference..is the approach correctJava Code:ArrayList<Integer> id=new ArrayList<Integer>(); public static void main(String args[]) { while(condition){ id=new ArrayList<Integer>(); id=d.search(); //gets 100,000 records d.processlogicindatabase(id); id=null; } }
- 03-15-2011, 03:44 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You don't need the list at all outside of the loop so simply declare it inside the loop.
Without knowing what either search() or process...() do I can't say at all what is going on.
My suggestion is to take heap dumps throughout the run and stick them through jhat or Eclipse MAT to see what is being built up and where.
- 03-15-2011, 06:26 PM #5
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
It seems to be a memory leak in either search method or processlogicindatabase method.
Re-visit these methods, I suggest.
-
Please use these variables to track the heap space to see where it is being used up.
Java Code:// Get current size of heap in bytes long heapSize = Runtime.getRuntime().totalMemory(); // Get maximum size of heap in bytes. The heap cannot grow beyond this size. // Any attempt will result in an OutOfMemoryException. long heapMaxSize = Runtime.getRuntime().maxMemory(); // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created. long heapFreeSize = Runtime.getRuntime().freeMemory();
- 03-16-2011, 06:46 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
rather code it this wayJava Code:ArrayList<Integer> id=new ArrayList<Integer>(); public static void main(String args[]) { while(condition){ id=new ArrayList<Integer>(); id=d.search(); //gets 100,000 records d.processlogicindatabase(id); id=null; } }
Java Code://if id is a method level variable then refer it to null; else leave it the way it is ArrayList<Integer> id; public static void main(String args[]) { while(condition){ id=d.search(); //gets 100,000 records d.processlogicindatabase(id); id=null; } }
- 03-16-2011, 08:22 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
No. When tracking possible memory leaks you use heap dumps.
The above will give you little useful information, never mind it involving scattering special code throughout your app.
And the above will achieve nothing in terms of memory usage, bar saving a few bytes on empty arraylists, which should be garbage collected anyway.
In addition, it's little different to the original code in that "id" is still unecessarily declared outside the loop.
Similar Threads
-
Java Heap space error using ArrayList.
By AcousticBruce in forum New To JavaReplies: 13Last Post: 01-03-2011, 01:06 PM -
Java Heap Space
By a.r.viswanath@gmail.com in forum EclipseReplies: 2Last Post: 07-02-2010, 05:34 AM -
Java heap space,
By babyboban in forum Advanced JavaReplies: 18Last Post: 01-14-2010, 12:22 PM -
Java Heap Space
By sandeeprao.techno in forum Advanced JavaReplies: 19Last Post: 10-30-2008, 11:27 AM -
Java heap space?
By javanewbie in forum New To JavaReplies: 1Last Post: 06-24-2008, 06:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks