increasing heapsize when you're running out of memory is rarely the answer (unless you require a single very large object in memory, and know for certain that it will never get any larger). Almost always you are having problems because of what is effectively a memory leak and increasing heapsize will only delay the crash, not prevent it.
Joined: Feb 08, 2005
I'm building a lot of objects dynamically. I read info from a text file into an arraylist. My program does fine for 200 items in the file. It crashes at 500 items. Once I have the items into the program, I parse the data into a 2 dimensional arraylist. Some of this data is removed because it doesn't meet certain conditions. Combinations are then made from the data items. So where I might have started with an arraylist of 100, my arraylist could now be 2200 or more. This is where it seems to crash. The goal is to then process these combinations and reduce the arraylist to a dozen combinations or so.
When I call arraylist item.remove() it removes the item from the arraylist. Doesn't the garbage collection run and clear those objects out of memory? Is there a trick help the garbage collector clean up all of those removed objects?
Since your program appears to involve a single pass through the data that at one point uses a lot of memory, I have no hesitation in recommending the simple increase your heap size solution as per Cameron's note. If we were talking about a service that ran many requests, you might then worry about memory leaks,etc.
A typical cause of memory use not being reduced when you do something like the arraylist remove call is additional references held elsewhere in the program. There are no tricks to the GC - if you get an out of memory error, the GC has tried its best already.
Since your algorithm seems to involve combinations, I would expect the memory requirements to grow much faster than linear with item count.
Joined: Feb 08, 2005
OK, that all makes sense. I thought Java handled all of the memory deallocation unlike C++.
If I have an arraylist that I'm not using should I iterate through it calling remove() to release the memory or leave it alone?
So do I enter the options before the program name or after?
java -Xms256m -Xmx512m MyProg
java MyProg -Xms256m -Xmx512m
Author and all-around good cowpoke
Joined: Mar 22, 2000
From this general tooldocs page - then select Basic Tools - then the referenc page for "java" according to your operating system to see all command line options explained. I dont understand this comment:
I thought Java handled all of the memory deallocation
Java will not magically deallocate memory; if you keep references to an object, it will not be discarded. Bill [ November 05, 2006: Message edited by: William Brogden ]