内存分配与回收策略

Java技术体系所提倡的自动内存管理最终可以归结为自动化地解决了两个问题:给对象分配内存以及回收分配给对象的内存,关于回收内存,前几篇博客已经介绍了虚拟机的垃圾收集器体系以及运行原理,现在我们讨论一下对象分配内存。
对象分配内存,往大方向讲,就是在堆上分配,对象主要分配在新生代的Eden区上,如果启动了本地线程分配缓冲,将按线程优先在TLAB上分配,少数情况下也可能会直接在老年代分配,总之分配的规则不是百分之百确定的。
1.对象优先在Eden分配
大多数情况下,对象在新生代Eden区中分配。当Eden区没有足够的空间进行分配,虚拟机将发起一次Minor GC。
在下面的代码中,尝试分配3个2MB大小和一个4MB大小的对象,在运行时通过-Xms20M -Xmx20M -Xmn10M这3个参数限制Java堆大小为20MB,不可扩展,其中10MB分配给新生代,剩下10MB分配给老年代。-XX:SurvivorRatio=8决定了新生代中Eden区与一个Survivor区的空间比例是8:1,从输出的结果也可以清晰地看到“eden space 8192K,from space 1024K,to space 1024K”的信息,新生代总可用空间为9216KB(Eden区+1个Survivor区总容量)。
执行testAllocation()中分配allocation4对象的语句时候会发生一次Minor GC,这次GC的结果是新生代6651KB变为148KB,而总内存占用量则几乎没有减少(因为allocation1, allocation2, allocation3三个对象都是存活的,虚拟机几乎没有找到可回收的对象)。这次GC发生的原因是给allocation4分配内存的时候,发现Eden已经被占用了6MB,剩余空间不足以分配allocation4所需的4MB 内存,因此发生Minor GC。GC期间虚拟机又发现自己有的3个2MB大小的对象全部无法放入Survivor(1MB),所以只好通过分配担保机制提前转移到老年代去。
这次GC结束后,4MB的allocation4对象顺利分配在Eden中,因此程序执行完的结果是Eden占用4MB(allocation4),Survivor空闲,老年代被占用6MB(被allocation1, allocation2, allocation3占用)。通过GC日志可以证实。

private static final int _1MB = 1024 * 1024;

/**
 * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC
  */
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
 }

运行结果:

[GC[DefNew: 6981K->478K(9216K), 0.0093822 secs] 6981K->6622K(19456K), 0.0094442 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
def new generation total 9216K, used 5066K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
eden space 8192K, 56% used [0x00000000f9a00000, 0x00000000f9e7af60, 0x00000000fa200000)
from space 1024K, 46% used [0x00000000fa300000, 0x00000000fa377af0, 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 2518K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 11% used [0x00000000fae00000, 0x00000000fb0758a8, 0x00000000fb075a00, 0x00000000fc2c0000)
No shared spaces configured.
2.大对象直接进入老年代
所谓的大对象是指,需要大量连续内存空间的Java对象,最典型的大对象就是那种很长的字符串以及数组。
虚拟机提供了一个-XX:PretenureSizeThreshold参数,令大于这个设置值的对象直接在老年代分配。这样做的目的是避免在Eden区以及两个Survivor区之间发生大量的内存复制。
执行下面代码方法后,Eden空间几乎没有被使用,而老年代的10MB空间被使用了40%,也就是allocation对象直接就被分配在老年代中,因为PretenureSizeThreshold设置成3MB。

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];  //直接分配在老年代中
}

运行结果:
Heap
def new generation total 9216K, used 1001K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
eden space 8192K, 12% used [0x00000000f9a00000, 0x00000000f9afa7b8, 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 2515K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 11% used [0x00000000fae00000, 0x00000000fb074cd0, 0x00000000fb074e00, 0x00000000fc2c0000)
No shared spaces configured.

3.长期存活的对象将进入老年代
既然虚拟机采用了分代收集思想管理内存,那么内存回收时就必须能识别哪些对象应放在新生代,哪些对象方在老年代。为了做到这一点,虚拟机给每个对象定义了一个对象年龄(Age)计数器。如果对象在Eden出生经过第一次Minor GC后仍然存活,你并且能够被Survivor容纳的话,将被移动到Survivor空间中,并且对象年龄设为1。对象在Survivor区中每“熬过”一次Monor GC,年龄就增加1岁,当他的年龄增加到一定程度(默认15岁),就将会被晋升到老年代中。对象晋升老年代的年龄阈值,可以通过参数MaxTenuringThreshold设置。
分别以MaxTenuringThreshold=1和MaxTenuringThreshold=15运行下面代码,allocation1随想需要6KB内存,Survivor空间可以容纳。当MaxTenuringThreshold=1时,allocation1对象在第二次GC发生时进入老年代,新生代已使用的内存GC后非常干净的变成0KB。而MaxTenuringThreshold=15时,第二次GC发生后,allocation1对象则还存留在新生代的Survivor空间,这是新生代仍然有空间被占用。

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];
}

以MaxTenuringThreshold=1参数运行的结果:

[GC[DefNew Desired survivor size 524288 bytes, new threshold 1 (max 1)
  • age 1: 496448 bytes, 496448 total
4933K->484K(9216K), 0.0042285 secs] 4933K->4580K(19456K), 0.0042905 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: 256 bytes, 256 total
4908K->0K(9216K), 0.0018028 secs] 9004K->4580K(19456K), 0.0018390 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4260K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
eden space 8192K, 52% used [0x00000000f9a00000, 0x00000000f9e28fd0, 0x00000000fa200000)
from space 1024K, 0% used [0x00000000fa200000, 0x00000000fa200100, 0x00000000fa300000)
to space 1024K, 0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
tenured generation total 10240K, used 4580K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
the space 10240K, 44% used [0x00000000fa400000, 0x00000000fa879038, 0x00000000fa879200, 0x00000000fae00000)
compacting perm gen total 21248K, used 2518K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 11% used [0x00000000fae00000, 0x00000000fb075890, 0x00000000fb075a00, 0x00000000fc2c0000)
No shared spaces configured.

以MaxTenuringThreshold=15参数运行的结果:

[GC[DefNew Desired survivor size 524288 bytes, new threshold 15 (max 15)
  • age 1: 496448 bytes, 496448 total
4933K->484K(9216K), 0.0082937 secs] 4933K->4580K(19456K), 0.0083582 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC[DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age 1: 256 bytes, 256 total
- age 2: 495656 bytes, 495912 total
4908K->484K(9216K), 0.0023305 secs] 9004K->4580K(19456K), 0.0023702 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4744K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
eden space 8192K, 52% used [0x00000000f9a00000, 0x00000000f9e28fd0, 0x00000000fa200000)
from space 1024K, 47% used [0x00000000fa200000, 0x00000000fa279128, 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 2518K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 11% used [0x00000000fae00000, 0x00000000fb075890, 0x00000000fb075a00, 0x00000000fc2c0000)
No shared spaces configured.

4.动态对象年龄判定
为了能更好的适应不同程序的内存状况,虚拟机并不是永远的要求对象年龄必须达到MaxTenuringThreshold才能晋升老年代,如果在Survivor空间中相同年龄所有对象大小的总和大于Survivor空间的一半,年龄大于或者等于该年龄的对象就可以直接进入老年代,无须等到MaxTenuringThreshold中要求的年龄。
执行testTenuringThreshold2()方法,并设置MaxTenuringThreshold=15,会发现结果中Survivor的空间占用仍为0%,而老年代比预期增加了6%,也就是说,allocation1, allocation2对象都直接进入了老年代,而没有等到15岁的临界年龄。因为这两个对象加起来已经达到512KB,并且他们是同年的,满足同年对象达到Survivor空间的一般规则。我们只要把其中一个注释掉,另一个调整到空间的50%一下,就发现他不会直接晋升到老年代中。

private static final int _1MB = 1024 * 1024;

/**
 * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=15
 * -XX:+PrintTenuringDistribution
 */
@SuppressWarnings("unused")
public static void testTenuringThreshold2() {
    byte[] allocation1, allocation2, allocation3, allocation4;
    allocation1 = new byte[_1MB / 4];   // allocation1+allocation2大于survivo空间一半
    allocation2 = new byte[_1MB / 4];  
    allocation3 = new byte[4 * _1MB];
    allocation4 = new byte[4 * _1MB];
    allocation4 = null;
    allocation4 = new byte[4 * _1MB];
}

运行结果:

[GC[DefNew Desired survivor size 524288 bytes, new threshold 1 (max 15)
  • age 1: 1014432 bytes, 1014432 total
5445K->990K(9216K), 0.0056121 secs] 5445K->5086K(19456K), 0.0056735 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
[GC[DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age 1: 256 bytes, 256 total
5414K->0K(9216K), 0.0022612 secs] 9510K->5086K(19456K), 0.0022986 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4260K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
eden space 8192K, 52% used [0x00000000f9a00000, 0x00000000f9e28fd0, 0x00000000fa200000)
from space 1024K, 0% used [0x00000000fa200000, 0x00000000fa200100, 0x00000000fa300000)
to space 1024K, 0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
tenured generation total 10240K, used 5085K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
the space 10240K, 49% used [0x00000000fa400000, 0x00000000fa8f7798, 0x00000000fa8f7800, 0x00000000fae00000)
compacting perm gen total 21248K, used 2518K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 11% used [0x00000000fae00000, 0x00000000fb0758b0, 0x00000000fb075a00, 0x00000000fc2c0000)
No shared spaces configured.

5.空间分配担保
在发生Minor GC之前,虚拟机会先检查老年代最大可用的连续空间是否大于新生代所有对象总空间,如果这个条件成立,那么Minor GC可以确保安全的。如果不成立,则虚拟机会查看HandlePromotionFailure设置值是否允许担保失败。如果允许,那么会继续检查老年代最大可用的连续空间是否大于历次晋升到老年代对象的平均大小,如果大于,将尝试着进行一次Minor GC,尽管这次Minor GC是有风险的;如果小于,或者HandlePromotionFailure设置不允许冒险,那这时要改为进行一次Full GC。
下面解释一下“冒险”是冒了什么风险,前面提到过,新生代使用复制收集算法,但为了内存利用率,只使用一个Survivor空间来作为轮换备份,因此当出现大量对象在Minor GC后仍然存活的情况,就需要老年代进行分配担保,把Survivor无法容纳的对象直接进入老年代,老年代进行这样的分配担保,前提是老年代本身还有容纳这些对象的剩余空间,一共有多少对象会活下来在实际完成完成内存回收之前无法明确知道,所以只好取之前每一次回收晋升到老年代对象容量大小的平均大小值作为经验值,与老年代剩余空间进行比较,决定是否进行Full GC 来让老年代腾出更多的空间。
取平均值进行比较其实仍然是一种动态概率手段,也就是说,如果某次Minor GC存活后的对象突增,远远高于平均值的话,依然会导致担保失败,如果出现了HandlePromotionFailure失败,那就只好重新发起一次Full GC。
JDK6 Update 24之后的规则变为只要老年代最大可用的连续空间大于新生代对象总大小或者历次晋升的平均大小就会进行Minor GC,否则将进行Full GC。

本文内容主要是本人学习周志明老师的《深入理解Java虚拟机》一书的学习笔记,仅作学习巩固整理知识点使用,不作他用,在此感谢周老师。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值