I ran this test with -Xmx256M to determine the max object size that I can create on heap
for (int m = 128;; m++) {
try {
byte[] a = new byte[m * 1024 * 1024];
} catch (OutOfMemoryError e) {
System.out.println(m + "M");
break;
}
}
and got 171M. Is there a way to calculate this size?
One way to get an estimate of an object's size in Java is to use getObjectSize(Object) method of the Instrumentation interface introduced in Java 5. As we could see in Javadoc documentation, the method provides “implementation-specific approximation” of the specified object's size.
If your -Xmx (maximum) is larger than the available memory (total memory to include any virtual memory) you will get a runtime failure if and only if your JVM processes actually tries to use more memory than the machine has.
The default maximum Java heap size is 256 MB.
Open a terminal window. Review the command output. The argument beginning with "-Xmx" will give you the value of the current Java heap space. In the example above, the value is 1024 MB, or 1 GB.
Is there a way to calculate this size?
No. The maximum allocatable object size depends on the amount of contiguous free space available in the heap. AFAIK, there's no practical way of finding out what that might be ... apart from doing what you are currently doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With