My program throws an OutOfMemory error and I don't know what to do.
The application dose a cycle (every second) reading the outputs of an
instrument. After a day the program stops in OutOfMemoryError blocking
the computer.
If I print the total memory every cycle, it starts with a heap of
7,806,976 bytes passing after a short time to 9,695,232 bytes until it
halts due to the error. The free memory floats between 1,600,000 to
2,300,000 bytes.
The strange thing is that if I execute the following simple "memory
eating" program
public class HeapTest {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
java.util.Vector v = new java.util.Vector();
while (true) {
long size = rt.freeMemory();
System.out.println("Total memory = " + rt.totalMemory()
+ ", free memory = " + size);
byte[] buffer = new byte[(int) size];
v.addElement(buffer);
}
}
}
the output is:
Total memory = 2031616, free memory = 1881280
Total memory = 3915776, free memory = 1936728
Total memory = 5853184, free memory = 1937392
Total memory = 7118848, free memory = 1265648
Total memory = 10543104, free memory = 3424392
Total memory = 12849152, free memory = 2306032
Total memory = 18952192, free memory = 6103024
Total memory = 23121920, free memory = 4169712
Total memory = 34013184, free memory = 10891248
Total memory = 41488384, free memory = 7475184
Total memory = 61018112, free memory = 19529712
Total memory = 66650112, free memory = 5547984
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
This means that the jvm can allocate up to 66,650,112 bytes, a lot more
that
the limit recahed in my application.
What could be the cause of the Out of Memory?
Thank you,
Matteo
Thomas Weidenfeller - 28 Apr 2005 10:50 GMT
> My program throws an OutOfMemory error and I don't know what to do.
Get a so called memory profiler and measure what eats up your memory.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
mdtorre@freemail.it - 28 Apr 2005 11:17 GMT
Could you suggest me some shareware or freeware profiler?
thank you.
Collin VanDyck - 28 Apr 2005 18:59 GMT
You can get a trial of JProbe or JProfiler.
I've not used the heap tools with the JProbe suite, but JProfiler has a
very very nice interface. You can filter down your view of the heap to
a certain class, package, or set of packages, and it will tell you how
many objects of type X are on the stack as well as show you deltas in
memory allocation.
"." - 28 Apr 2005 22:42 GMT
> My program throws an OutOfMemory error and I don't know what to do.
> The application dose a cycle (every second) reading the outputs of an
[quoted text clipped - 42 lines]
> that
> the limit recahed in my application.
You can change the heap size for any given instance of the JVM. It could
be that the way your application is being launched restricts the JVM heap.
Look for -Xmx settings on the java command.
Another possibility is that something is allocating one massive amount of
memory. So instead of linear growth, like your test app, it is growing
expontentially.
> What could be the cause of the Out of Memory?
Depending on how well you know your app, you could do a code inspection
and figure out what is holding on to so much memory or you can get a tool
to do it for you. I'd use something like JProbe but this is available to
me. You might not want to go out and buy it.
If you are just going to scan the code by hand then look for anything that
holds onto references. Like your example above, are you pushing things
into a vector, collection, list, etc. and never removing them?

Signature
Send e-mail to: darrell dot grainger at utoronto dot ca
Robert - 28 Apr 2005 23:06 GMT
You realize that from the code above you're never releasing that mem
right? You just keep adding more and more to the vector.
OutOfMemoryException is the only logical solution here. You usually do
something with the data and then release it or write it to a file or a
DB. Something. You don't just keep loading data into memory. The heap
idea above may answer your question better.
mdtorre@freemail.it - 29 Apr 2005 14:59 GMT
> You realize that from the code above you're never releasing that mem
> right? You just keep adding more and more to the vector.
> OutOfMemoryException is the only logical solution here. You usually do
> something with the data and then release it or write it to a file or a
> DB. Something. You don't just keep loading data into memory. The heap
> idea above may answer your question better.
I put the code above, that is not my program, only to show that the jvm
can allocate up to 66,650,112 bytes but in my application the jvm goes
in out of memory error when are allocated only 9,695,232 bytes. So I
don't understand the reason of it going out of memory.
Patricia Shanahan - 29 Apr 2005 16:15 GMT
>>You realize that from the code above you're never releasing that mem
>>right? You just keep adding more and more to the vector.
[quoted text clipped - 16 lines]
> in out of memory error when are allocated only 9,695,232 bytes. So I
> don't understand the reason of it going out of memory.
Here are a couple of idea:
1. The failing program attempts a single allocation:
double[] d = new double[10000000];
2. They are running with different memory limit settings.
What does Runtime.getRuntime().maxMemory() return in each case?
Patricia