jvm-JDK8对象堆内存分配,大对象直接分配老年代及GC测试

环境设置

jvm options:  -XX:+PrintGCDetails  -Xms30M -Xmx30M ,新生代10M(eden8M+from1M+to1M),老年代20M

 

测试一、分配2M内存。

分配2M内存成功,但分配后eden基本占满(b1及其它对象共占eden98%),即将发生Yong GC


  public static void main(String[] args) {
      byte[] b1= new byte[2*1024 * 1024];
     
  }
Heap
 PSYoungGen      total 9216K, used 8040K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 98% used [0x00000000ff600000,0x00000000ffdda340,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 20480K, used 0K [0x00000000fe200000, 0x00000000ff600000, 0x00000000ff600000)
  object space 20480K, 0% used [0x00000000fe200000,0x00000000fe200000,0x00000000ff600000)
 Metaspace       used 3426K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 369K, capacity 388K, committed 512K, reserved 1048576K

测试二、分配3M

分配前endn空间已不足,触发yong GC,新生代5665K->996K回收了4669K,总堆5665K->1678K回收了3987K,共有4669-3987=681K进入老年代,GC后在eden分配3M内存成功(eden38%=3113K基本为3M的b1对象)

  public static void main(String[] args) {
      byte[] b1= new byte[3*1024 * 1024];
      
}
[GC (Allocation Failure) [PSYoungGen: 5665K->996K(9216K)] 5665K->1678K(29696K), 0.0079674 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 9216K, used 4151K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 38% used [0x00000000ff600000,0x00000000ff914930,0x00000000ffe00000)
  from space 1024K, 97% used [0x00000000ffe00000,0x00000000ffef93c0,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 20480K, used 681K [0x00000000fe200000, 0x00000000ff600000, 0x00000000ff600000)
  object space 20480K, 3% used [0x00000000fe200000,0x00000000fe2aa610,0x00000000ff600000)
 Metaspace       used 3428K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 369K, capacity 388K, committed 512K, reserved 1048576K

测试三、分配4M。

分配前endn空间已不足,GC前判断即便将这78%对象全部回收也不足以或已达到设定阈值,新对象b1不会分配到新生代eden,而是直接分配到老年代(4M=4096K),新生代也不必yong GC

  public static void main(String[] args) {
      byte[] b1= new byte[4*1024 * 1024];
  }
Heap
 PSYoungGen      total 9216K, used 6443K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 78% used [0x00000000ff600000,0x00000000ffc4af70,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 20480K, used 4096K [0x00000000fe200000, 0x00000000ff600000, 0x00000000ff600000)
  object space 20480K, 20% used [0x00000000fe200000,0x00000000fe600010,0x00000000ff600000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 344K, capacity 392K, committed 512K, reserved 1048576K

测试四、分配4M(分两次进行)

b1成功分配到eden(由测试1可知),但分配b2时已eden区已不足,会进行yong GC,将回收eden区对象(b1及其它大于from to区大小(1M)的对象不会等到年龄15,而是直接进入老年代),GC后足以分配b2(8192*26%=2130K基本为2M的b2对象)

public static void main(String[] args) {
      byte[] b1= new byte[2*1020 * 1024];
      byte[] b2= new byte[2*1020 * 1024];
  }
 
[GC (Allocation Failure) [PSYoungGen: 7704K->996K(9216K)] 7704K->3606K(29696K), 0.0059974 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 9216K, used 3202K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 26% used [0x00000000ff600000,0x00000000ff8277d0,0x00000000ffe00000)
  from space 1024K, 97% used [0x00000000ffe00000,0x00000000ffef93b0,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 20480K, used 2609K [0x00000000fe200000, 0x00000000ff600000, 0x00000000ff600000)
  object space 20480K, 12% used [0x00000000fe200000,0x00000000fe48c620,0x00000000ff600000)
 Metaspace       used 3395K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 364K, capacity 388K, committed 512K, reserved 1048576K

测试五、分配20M内存(分两次)。

尝试了两次full GC,老年代对象回收后还是不足以20M的b1分配,最终抛出堆OOM

  public static void main(String[] args) {
      byte[] b1= new byte[10*1024 * 1024];
      byte[] b2= new byte[10*1024 * 1024];
  }

 

[GC (Allocation Failure) [PSYoungGen: 5828K->1012K(9216K)] 16068K->11793K(29696K), 0.0143019 secs] [Times: user=0.00 sys=0.00, real=0.02 secs] 
[GC (Allocation Failure) [PSYoungGen: 1012K->1000K(9216K)] 11793K->11788K(29696K), 0.0024642 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 1000K->0K(9216K)] [ParOldGen: 10788K->11758K(20480K)] 11788K->11758K(29696K), [Metaspace: 3419K->3419K(1056768K)], 0.0183525 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(9216K)] 11758K->11758K(29696K), 0.0005769 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(9216K)] [ParOldGen: 11758K->11741K(20480K)] 11758K->11741K(29696K), [Metaspace: 3419K->3419K(1056768K)], 0.0183187 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at com.construn.vehicle.study.util.Test.main(Test.java:12)
Heap
 PSYoungGen      total 9216K, used 218K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 2% used [0x00000000ff600000,0x00000000ff636b58,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 20480K, used 11741K [0x00000000fe200000, 0x00000000ff600000, 0x00000000ff600000)
  object space 20480K, 57% used [0x00000000fe200000,0x00000000fed77628,0x00000000ff600000)
 Metaspace       used 3453K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 372K, capacity 388K, committed 512K, reserved 1048576K

测试六、分配20M内存(分两次),但b1置空。

full GC会回收掉b1,然后老年代分配b2成功。

  public static void main(String[] args) {
      byte[] b1= new byte[10*1024 * 1024];
      b1=null;
      byte[] b2= new byte[10*1024 * 1024];
  }
[GC (Allocation Failure) [PSYoungGen: 5498K->1012K(9216K)] 15738K->11760K(29696K), 0.0316375 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
[GC (Allocation Failure) [PSYoungGen: 1012K->1012K(9216K)] 11760K->11776K(29696K), 0.0180783 secs] [Times: user=0.00 sys=0.00, real=0.02 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 1012K->0K(9216K)] [ParOldGen: 10763K->1484K(20480K)] 11776K->1484K(29696K), [Metaspace: 3280K->3280K(1056768K)], 0.0106351 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 9216K, used 164K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 2% used [0x00000000ff600000,0x00000000ff629140,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 20480K, used 11724K [0x00000000fe200000, 0x00000000ff600000, 0x00000000ff600000)
  object space 20480K, 57% used [0x00000000fe200000,0x00000000fed731b8,0x00000000ff600000)
 Metaspace       used 3319K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 361K, capacity 388K, committed 512K, reserved 1048576K
 
 
 

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值