1.示例代码:
// JVM 配置: -Xmx20m -Xms20m -Xmn10m -XX:+PrintGCDetails
public class HellocGC {
static int _1MB = 1024 * 1024;
public static void main(String[] args) {
byte[] b1 = new byte[2 * _1MB];
//年轻代大小为10m,为什么执行完b2之后,会进行minor GC?
byte[] b2 = new byte[2 * _1MB];
byte[] b3 = new byte[2 * _1MB];
byte[] b5 = new byte[4 * _1MB];
}
}
2.GC日志:
[GC (Allocation Failure) [PSYoungGen: 6452K->1012K(9216K)] 6452K->3398K(19456K), 0.0024151 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 9216K, used 5512K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 8192K, 54% used [0x00000000ff600000,0x00000000ffa64eb8,0x00000000ffe00000)
from space 1024K, 98% used [0x00000000ffe00000,0x00000000ffefd210,0x00000000fff00000)
to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
ParOldGen total 10240K, used 6482K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 63% used [0x00000000fec00000,0x00000000ff254800,0x00000000ff600000)
Metaspace used 3134K, capacity 4556K, committed 4864K, reserved 1056768K
class space used 328K, capacity 392K, committed 512K, reserved 1048576K
Disconnected from the target VM, address: '127.0.0.1:64658', transport: 'socket'
3.环境配置:
JDK版本:1.8
收集器: PS +PO
新生代大小:10M 老年代大小:10M
4.问题:
- 1.为什么运行第二行代码之后进行了Minor GC? 我新生代才分配了4M内存啊?
- 2.通过GC日志分析,为什么GC前新生代占用为 6452k, 我才分配了4M,应该是 4096k啊?
- 3.通过GC日志分析,老年代占用空间约为 6428k,新生代为 5512k。 我期望的值是 老年代占用约为:4M,因为第一个Minor GC将 b1 b2两个对象放到了老年代故为 4M。 新生代约为:6M. 显示的结果和我期望的是相反的,这走的是分配担保机制吗?如果是分配担保机制为什么没有触发一次Minor GC,这是什么原因?
在线等待大佬回答,帮忙解惑,非常感谢。