[GC (Allocation Failure) [ParNew: 7989K->424K(9216K), 0.0037834 secs] 7989K->6568K(19456K), 0.0038147 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] Heap par new generation total 9216K, used 4685K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000) eden space 8192K, 52% used [0x00000007bec00000, 0x00000007bf0290f0, 0x00000007bf400000) from space 1024K, 41% used [0x00000007bf500000, 0x00000007bf56a3a8, 0x00000007bf600000) to space 1024K, 0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000) tenured generation total 10240K, used 6144K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000) the space 10240K, 60% used [0x00000007bf600000, 0x00000007bfc00030, 0x00000007bfc00200, 0x00000007c0000000) Metaspace used 3242K, capacity 4496K, committed 4864K, reserved 1056768K class space used 359K, capacity 388K, committed 512K, reserved 1048576K
Process finished with exit code 0
实验分析:
通过上面的GC日志我们可以看出一开始出现了 MinorGC, 引起GC的原因是 内存分配失败,因为分配allocation4的时候,Eden区已经没有足够的区域来分配了,所以发生了本次 MinorGC ,经过 MinorGC 之后新生代的已使用容量从7989K减少到了424K,然而整个堆的内存总量却几乎没有减少,原因就是,由于发现新生代没有可以回收的对象,所以不得不使用内存担保将allocation1~3 三个对象提前转移到老年代。此时再在 Eden 区域为 allocation4 分配 4MB 的空间,因此最后我们发现 Eden 区域占用了 4MB,老年代占用了 6MB
实验二:使用Parallel Scavenge+Serial Old的组合进行内存回收
对象优先在 Eden 区分配
实验结果
1 2 3 4 5 6 7 8 9
Heap PSYoungGen total 9216K, used 8192K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000) eden space 8192K, 100% used [0x00000007bf600000,0x00000007bfe00000,0x00000007bfe00000) from space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000) to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000) ParOldGen total 10240K, used 4096K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000) object space 10240K, 40% used [0x00000007bec00000,0x00000007bf000010,0x00000007bf600000) Metaspace used 3311K, capacity 4496K, committed 4864K, reserved 1056768K class space used 369K, capacity 388K, committed 512K, reserved 1048576K
Heap par new generation total 9216K, used 2936K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000) eden space 8192K, 35% used [0x00000007bec00000, 0x00000007beede130, 0x00000007bf400000) from space 1024K, 0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000) to space 1024K, 0% used [0x00000007bf500000, 0x00000007bf500000, 0x00000007bf600000) tenured generation total 10240K, used 4096K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000) the space 10240K, 40% used [0x00000007bf600000, 0x00000007bfa00010, 0x00000007bfa00200, 0x00000007c0000000) Metaspace used 3278K, capacity 4496K, committed 4864K, reserved 1056768K class space used 364K, capacity 388K, committed 512K, reserved 1048576K
实验分析
通过上面的堆的内存占用情况很容易看出我们分配的4MB大小的对象直接被放到了老年代
实验四: 使用参数-XX:MaxTenuringThreshold=1来设置进入老年代的对象年龄
长期存活的对象将进入老年代
实验结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[GC (Allocation Failure) [DefNew Desired survivor size 524288 bytes, new threshold 1 (max 1) - age 1: 749112 bytes, 749112 total : 6868K->731K(9216K), 0.0039484 secs] 6868K->4827K(19456K), 0.0039697 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC (Allocation Failure) [DefNew Desired survivor size 524288 bytes, new threshold 1 (max 1) - age 1: 1496 bytes, 1496 total : 4911K->1K(9216K), 0.0012023 secs] 9007K->4706K(19456K), 0.0012150 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap def new generation total 9216K, used 4316K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000) eden space 8192K, 52% used [0x00000007bec00000, 0x00000007bf036cc8, 0x00000007bf400000) from space 1024K, 0% used [0x00000007bf400000, 0x00000007bf4005d8, 0x00000007bf500000) to space 1024K, 0% used [0x00000007bf500000, 0x00000007bf500000, 0x00000007bf600000) tenured generation total 10240K, used 4704K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000) the space 10240K, 45% used [0x00000007bf600000, 0x00000007bfa98328, 0x00000007bfa98400, 0x00000007c0000000) Metaspace used 3288K, capacity 4496K, committed 4864K, reserved 1056768K class space used 364K, capacity 388K, committed 512K, reserved 1048576K