简体   繁体   中英

Why does my Java heap dump show only half the heap?

I'm intentionally adding items to a data structure until Java throws an OutOfMemoryError , and grabbing a heap dump using -XX:+HeapDumpOnOutOfMemoryError . I'm using -Xmx100M , so I expected the heap dump would show nearly 100MB of live objects, but instead it shows only 46MB.

Why would Java throw OOM with a 100MB heap if there are only 46MB of live objects?

(I realize there's a good chance I haven't given enough information to answer this question, but I'm not sure what's relevant. Happy to add more info as needed.)

EDIT: The end of the -verbose:gc output:

 [Full GC (Ergonomics) [PSYoungGen: 1056K->64K(2560K)] [ParOldGen: 7116K->7116K(7168K)] 8173K->7180K(9728K), [Metaspace: 5981K->5981K(1056768K)], 0.0071141 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
 [Full GC (Allocation Failure) [PSYoungGen: 64K->0K(2560K)] [ParOldGen: 7116K->7075K(7168K)] 7180K->7075K(9728K), [Metaspace: 5981K->5974K(1056768K)], 0.0190392 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 java.lang.OutOfMemoryError: Java heap space

Heap is split into two sections young generation and old generation. You get OOM when eg the old generation is full or fragmented in such way that it is impossible to promote object from young generation to the old (and probably others).

If you log GC usage using eg -verbose:gc -XX:+PrintGCDetails -Xloggc:gc.log then you will see each generation sizes.

You can see how to tune generation sizes here https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/sizing.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM