File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Out of memory error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Out of memory error" Watch "Out of memory error" New topic
Author

Out of memory error

R Jarman
Greenhorn

Joined: Feb 08, 2005
Posts: 27
While running I'm getting the following exception.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Is there something I can enter when I run my program to increase the heap size?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Up youre heap size!

All you're doing is garbage collecting. When over 98% of the JVM time is being used to collect garbage. the OutOfMemory error gets generated. It's deadly.

These java switches will increase your heap size:

-Xms256m -Xmx512m

This will make the initial heap size 256, and the max heap size 512.

Of course, perhaps examining how many objects are being created and gargabe collected might not be a bad idea either.

Cheers!

-Cameron McKenzie
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
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.


42
R Jarman
Greenhorn

Joined: Feb 08, 2005
Posts: 27
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?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
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.

Bill


Java Resources at www.wbrogden.com
R Jarman
Greenhorn

Joined: Feb 08, 2005
Posts: 27
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

or

java MyProg -Xms256m -Xmx512m
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Out of memory error
 
Similar Threads
Help with Garbage Collection
my webcrawler throws: java.lang.OutOfMemoryError: Java heap space
static block
startNodeManager.sh and startWeblogic.sh doesn't start to execute
WA #1.....word association