Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Out of memory error

 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic