Java虚拟机(三):内存分配与回收策略

     对象的分配, 就是在堆上,主要是新生代Eden区上分配对象空间,如果启动了本地线程分配缓冲,将按照线程优先在TLAB上分配。少数情况下也可能分配在老年代中。
1、对象优先在Eden区分配
       大多数情况对象在新生代Eden区分配,当Eden区没有足够空间进行分配时,虚拟机将发起一次Minor GC。
     VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 。 设置java堆大小限制为20M,新生代10M,         Eden区和Survivor区的  比例是8,即Eden区为8M,每个Survivor区大小为1M,并打印垃圾回收日志。
代码:
package com.xl.jvm;
public class TestGc {
 
 private static final int _1MB = 1024 * 1024;
 /**
  * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
   */
 public static void testAllocation() {
   byte[] allocation1, allocation2, allocation3, allocation4;
   allocation1 = new byte[2 * _1MB];
   allocation2 = new byte[2 * _1MB];
   allocation3 = new byte[2 * _1MB];
   allocation4 = new byte[4 * _1MB];  // 出现一次Minor GC
  }
 public static void main(String[] args) {
  testAllocation();
 }
}
结果:
[GC[DefNew: 6816K->474K(9216K), 0.0048603 secs] 6816K->6618K(19456K), 0.0049014 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
 def new generation   total 9216K, used 5062K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
  eden space 8192K,  56% used [0x00000000f9a00000, 0x00000000f9e7af60, 0x00000000fa200000)
  from space 1024K,  46% used [0x00000000fa300000, 0x00000000fa376a28, 0x00000000fa400000)
  to   space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
 tenured generation   total 10240K, used 6144K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
   the space 10240K,  60% used [0x00000000fa400000, 0x00000000faa00030, 0x00000000faa00200, 0x00000000fae00000)
 compacting perm gen  total 21248K, used 2461K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
   the space 21248K,  11% used [0x00000000fae00000, 0x00000000fb0675e8, 0x00000000fb067600, 0x00000000fc2c0000)
No shared spaces configured.
2、大对象直接进入老年代
      所谓大对象就是指需要大量连续内存空间的Java对象,最典型的大对象就是那种很长的字符串以及数组。这样做的目的是避免Eden区及两个Servivor之间发生大量的内存复制。
    VM参数:-XX:PretenureSizeThreshold=3145728 参数设定超过对象超过多少时,分配到老年代中,此例为3M(3*1024*1024)。
代码:
  
 package com.xl.jvm;

public class TestGc {
 
 private static final int _1MB = 1024 * 1024;
 /**
  * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
  * -XX:PretenureSizeThreshold=3145728
  */
 public static void testPretenureSizeThreshold() {
  byte[] allocation;
  allocation = new byte[4 * _1MB];  //直接分配在老年代中
 }
 public static void main(String[] args) {
  testPretenureSizeThreshold();
 }
}

结果:
Heap
 def new generation   total 9216K, used 836K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
  eden space 8192K,  10% used [0x00000000f9a00000, 0x00000000f9ad10d8, 0x00000000fa200000)
  from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
  to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
 tenured generation   total 10240K, used 4096K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
   the space 10240K,  40% used [0x00000000fa400000, 0x00000000fa800010, 0x00000000fa800200, 0x00000000fae00000)
 compacting perm gen  total 21248K, used 2458K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
   the space 21248K,  11% used [0x00000000fae00000, 0x00000000fb066a70, 0x00000000fb066c00, 0x00000000fc2c0000)
No shared spaces configured.
3、 长期存活的对象将进入老年代
      如果对象在Eden区出生并且尽力过一次Minor GC后仍然存活,并且能够被Servivor容纳,将被移动到Servivor空间中,并且把对象年龄设置成为1.对象在Servivor区中每熬过一次Minor GC,年龄就增加1岁,当它的年龄增加到一定程度(默认15岁),就将会被晋级到老年代中。
   代码:
package com.xl.jvm;
public class TestGc {
 private static final int _1MB = 1024 * 1024;
 /**
  * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1
  * -XX:+PrintTenuringDistribution
  */
 @SuppressWarnings("unused")
 public static void testTenuringThreshold() {
  byte[] allocation1, allocation2, allocation3;
  allocation1 = new byte[_1MB / 4];  // 什么时候进入老年代决定于XX:MaxTenuringThreshold设置
  allocation2 = new byte[4 * _1MB];
  allocation3 = new byte[4 * _1MB];
  allocation3 = null;
  allocation3 = new byte[4 * _1MB];
 }
 public static void main(String[] args) {
  testTenuringThreshold();
 }
}
结果:
[GC[DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age   1:     748104 bytes,     748104 total
: 5024K->730K(9216K), 0.0038710 secs] 5024K->4826K(19456K), 0.0039143 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC[DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age   1:        120 bytes,        120 total
: 4990K->0K(9216K), 0.0026329 secs] 9086K->4826K(19456K), 0.0026618 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
 def new generation   total 9216K, used 4424K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
  eden space 8192K,  54% used [0x00000000f9a00000, 0x00000000f9e51f90, 0x00000000fa200000)
  from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa200078, 0x00000000fa300000)
  to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
 tenured generation   total 10240K, used 4826K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
   the space 10240K,  47% used [0x00000000fa400000, 0x00000000fa8b6a58, 0x00000000fa8b6c00, 0x00000000fae00000)
 compacting perm gen  total 21248K, used 2461K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
   the space 21248K,  11% used [0x00000000fae00000, 0x00000000fb0675d8, 0x00000000fb067600, 0x00000000fc2c0000)
No shared spaces configured.


参考资料:《深入理解Java虚拟机》

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值