I tried to see how -Xmx
and -Xms
parameters impact on my program and check how much memory does my process consume.
I wrote a simple program but am not able to reason out the results. Kindly help.
public static void main( String[] args ) {
char[] array = new char[69926904];
}
I ran with parameters -Xms5M -Xmx200M
. Ideally, since a character takes 2 bytes, it should hold 100M characters before exceeding the memory limit. Even if we say, few space is getting used for pointer and length, I don't know, why it is throwing error after 69926904 length.
Thanks.
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.
1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.
The easy way to monitor Heap usage is by using a commercial APM (Application Performance management tool) such as CA Wily APM, AppDynamics, New Relic, Riverbed, etc. APM tools not only monitor the heap usage, but you can also configure the tool to Alert you when Heap usage is not normal.
Read carefully JVM Garbage Collection Tuning Guide about Generational Heap, and it will hopefully answer your question.
Run Java with -XX:+PrintGCDetails
option, and everything should become clear:
Heap
PSYoungGen total 3584K, used 294K [0x00000000fbd60000, 0x00000000fc160000, 0x0000000100000000)
eden space 3072K, 9% used [0x00000000fbd60000,0x00000000fbda9860,0x00000000fc060000)
from space 512K, 0% used [0x00000000fc0e0000,0x00000000fc0e0000,0x00000000fc160000)
to space 512K, 0% used [0x00000000fc060000,0x00000000fc060000,0x00000000fc0e0000)
PSOldGen total 136576K, used 136576K [0x00000000f3800000, 0x00000000fbd60000, 0x00000000fbd60000)
object space 136576K, 100% used [0x00000000f3800000,0x00000000fbd60000,0x00000000fbd60000)
PSPermGen total 21248K, used 2595K [0x00000000ee600000, 0x00000000efac0000, 0x00000000f3800000)
object space 21248K, 12% used [0x00000000ee600000,0x00000000ee888db0,0x00000000efac0000)
Your 200M Java Heap consists of 2 generations: 1/3 (66.7M) is YoungGen and 2/3 (133.3M) is OldGen.
-XX:NewRatio
option allows to change the proportion, but the default value of 2 means that YoungGen will reserve 1/(2+1) part of the heap.
Java objects cannot span generations, so the maximum size of object cannot be larger than the largest generation. In your case the largest generation is OldGen: 136576K = 139853824 which is exactly the size of char[69926904]
(16 bytes header + 2 * 69926904 bytes of data).
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