LPD LPD - 1 year ago 37
Java Question

Java process memory check test

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.

Answer Source

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).