M
mdtorre
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
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