深入理解Java虚拟机之第3章-3.6内存分配与回收策略

内存分配与回收策略

  • 对象的内存分配,往大方向讲,就是在堆上分配(但也可能经过JIT编译后被拆散为标量类型并间接地栈上分配),对象主要分配在新生代的Eden区上,如果启动了本地线程分配缓冲,将按线程优先在TLAB上分配。少数情况下也可能会直接分配在老年代中,分配的规则并不是百分之百固定的,其细节取决于当前使用的是哪一种垃圾收集器组合,还有虚拟机中与内存相关的参数的设置。

1、对象优先在Eden分配

  • 大多数情况下,对象在新生代Eden区中分配。当Eden区没有足够空间进行分配时,虚拟机将发起一次Minor GC。
  • 以下代码清单尝试分配3个2MB大小和1个4MB大小的对象,在运行时通过-Xms20M、-Xmx20M、-Xmn10M这3个参数限制了Java堆大小为20MB,不可扩展,其中10MB分配给新生代,剩下的10MB分配给老年代。-XX:SurvivorRatio=8决定了新生代中Eden区与一个Survivor区的空间比例是8:1。
  • 执行testAllocation()中分配allocation4对象的语句时会发生一次Minor GC。这次GC发生的原因是给allocation4分配内存的时候,发现Eden已经被占用了6MB,剩余空间已不足以分配allocation4所需的4MB内存,因此发生Minor GC。GC期间虚拟机又发现已有的3个2MB大小的对象全部无法放入Survivor空间(Survivor空间只有1MB大小),所以只好通过分配担保机制提前转移到老年代区。
  • 新生代GC(Minor GC):指发生在新生代的垃圾收集动作,因为Java对象大多都具备招生夕灭的特性,所以Minor GC非常频繁,一般回收速度也比较快。
  • 老年代GC(Major GC/Full GC):指发生在老年代的GC,出现了Major GC,经常会伴随至少一次的Minor GC(但非绝对的,在Parallel Scavenge收集器的收集策略里就有直接进行Major GC的策略选择过程)。Major GC的速度一般会比Minor GC慢10倍以上。
package com.allocation;

/**
 * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
 * @author Administrator
 *
 */
public class TestAllocation {
	private static final int _1MB = 1024 * 1024;
	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];
	}
	
	public static void main(String[] args) {
		testAllocation();
	}
}

运行结果:

[GC (Allocation Failure) [DefNew: 7127K->561K(9216K), 0.0183748 secs] 7127K->6705K(19456K), 0.0324052 secs] [Times: user=0.00 sys=0.00, real=0.03 secs] 
Heap
 def new generation   total 9216K, used 4739K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  51% used [0x00000000fec00000, 0x00000000ff014930, 0x00000000ff400000)
  from space 1024K,  54% used [0x00000000ff500000, 0x00000000ff58c640, 0x00000000ff600000)
  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 tenured generation   total 10240K, used 6144K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  60% used [0x00000000ff600000, 0x00000000ffc00030, 0x00000000ffc00200, 0x0000000100000000)
 Metaspace       used 2756K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 295K, capacity 386K, committed 512K, reserved 1048576K

2、大对象直接进入老年代

  • 所谓的大对象是指,需要大量连续内存空间的Java对象,最典型的大对象就是那种很长的字符串以及数组(例子中的byte[]数组就是典型的大对象)。大对象堆虚拟机的内存分配来说就是一个坏消息,经常出现大对象容易导致内存还有不少空间时就提前触发垃圾收集以获取足够的连续空间来“安置”它们。
  • 虚拟机提供了一个-XX:PretenureSizeThreshold参数,令大于这个设置值的对象直接在老年代分配。以下代码中testPretenureSizeThreshold()方法后,可以看到Eden空间几乎没有被使用,而老年代的10MB空间被使用了40%,也就是4MB的allocation对象直接就分配在老年代中,这是因为PretenureSizeThreshold被设置为3MB(就是3145728,这个参数不能像-Xmx之类的参数一样直接写3MB)。
  • PretenureSizeThreshold参数只对Serial和ParNew两款收集器有效,Parallel Scavenge收集器不认识这个参数,Parallel Scavenge收集器一般并不需要设置。如果遇到必须使用此参数的场合,可以考虑ParNew加CMS的收集器组合。
package com.pretenure;

/**
 * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
 * -XX:PretenureSizeThreshold=3145728 -XX:+UseSerialGC
 * @author Administrator
 *
 */
public class PretenureSizeThreshold {
	private static final int _1MB = 1024 * 1024;
	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 1147K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  14% used [0x00000000fec00000, 0x00000000fed1ef98, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 4096K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  40% used [0x00000000ff600000, 0x00000000ffa00010, 0x00000000ffa00200, 0x0000000100000000)
 Metaspace       used 2757K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 295K, capacity 386K, committed 512K, reserved 1048576K
 

3、长期存活的对象将进入老年代

  • 既然虚拟机采用了分代收集的思想来管理内存,那么内存回收时就必须能识别哪些对象应放在新生代,哪些对象应放在老年代。为了做到这点,虚拟机给每个对象定义了一个对象年龄计数器。如果对象在Eden出生并经过第一次Minor GC后仍然存活,并且能被Survivor容纳的话,将被移动到Survivor空间中,并且对象年龄设为1。对象在Survivor区中每“熬过”一次Minor GC,年龄就增加1岁,当它的年龄增加到一定程度(默认为15岁),就将会被晋升到老年代中。对象晋升老年代的年龄阈值,可以通过参数-XX:MaxTenuringThreshold设置。
  • 以下代码清单中,在allocation3分配内存时,发生第一次Minor GC,allocation1对象需要256KB内存,Survivor空间可以容纳,allocation1的年龄为1。allocation2对象需要4MB内存,Survivor空间容纳不了就要放在老年代中。第一次GC之后,Eden中存储4MB的allocation3对象,Survivor中存储256KB的allocation1对象,老年代存储4MB的allocation2对象。在执行最后一条语句给allocation3分配内存时,发生第二次Minor GC。当虚拟机参数-XX:MaxTenuringThreshold=1时,allocation1进入老年代,当虚拟机参数-XX:MaxTenuringThreshold=15时,allocation1仍然在新生代中。验证这个例子时,使用1.6的jdk版本才生效。
package com.tenuringdistribution;

/**
 * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 
 * -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution
 * @author Administrator
 *
 */
public class TenuringThreshold {
	private static final int _1MB = 1024 * 1024;
	public static void testTenuringThreshold() {
		byte[] allocation1, allocation2, allocation3;
		allocation1 = new byte[_1MB / 4];
		allocation2 = new byte[4 * _1MB];
		allocation3 = new byte[4 * _1MB];
		allocation3 = null;
		allocation3 = new byte[4 * _1MB];
	}
	
	public static void main(String[] args) {
		testTenuringThreshold();
	}
}

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

[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
– age   1:     415696 bytes,     415696 total
: 4695K->405K(9216K), 0.0059055 secs] 4695K->4501K(19456K), 0.0059412 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
– age   1:        136 bytes,        136 total
: 4665K->0K(9216K), 0.0011338 secs] 8761K->4501K(19456K), 0.0012204 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 9216K, used 4260K [0x33060000, 0x33a60000, 0x33a60000)
  eden space 8192K,  52% used [0x33060000, 0x33488fe0, 0x33860000)
  from space 1024K,   0% used [0x33860000, 0x33860088, 0x33960000)
  to   space 1024K,   0% used [0x33960000, 0x33960000, 0x33a60000)
 tenured generation   total 10240K, used 4501K [0x33a60000, 0x34460000, 0x34460000)
   the space 10240K,  43% used [0x33a60000, 0x33ec5710, 0x33ec5800, 0x34460000)
 compacting perm gen  total 12288K, used 375K [0x34460000, 0x35060000, 0x38460000)
   the space 12288K,   3% used [0x34460000, 0x344bdc90, 0x344bde00, 0x35060000)
    ro space 10240K,  51% used [0x38460000, 0x38993000, 0x38993000, 0x38e60000)
    rw space 12288K,  55% used [0x38e60000, 0x394fe4f8, 0x394fe600, 0x39a60000)
 

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

[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
– age   1:     415696 bytes,     415696 total
: 4695K->405K(9216K), 0.0061920 secs] 4695K->4501K(19456K), 0.0062289 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
– age   1:        136 bytes,        136 total
– age   2:     415488 bytes,     415624 total
: 4665K->405K(9216K), 0.0010386 secs] 8761K->4501K(19456K), 0.0010710 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 9216K, used 4665K [0x33060000, 0x33a60000, 0x33a60000)
  eden space 8192K,  52% used [0x33060000, 0x33488fe0, 0x33860000)
  from space 1024K,  39% used [0x33860000, 0x338c5788, 0x33960000)
  to   space 1024K,   0% used [0x33960000, 0x33960000, 0x33a60000)
 tenured generation   total 10240K, used 4096K [0x33a60000, 0x34460000, 0x34460000)
   the space 10240K,  40% used [0x33a60000, 0x33e60010, 0x33e60200, 0x34460000)
 compacting perm gen  total 12288K, used 375K [0x34460000, 0x35060000, 0x38460000)
   the space 12288K,   3% used [0x34460000, 0x344bdc90, 0x344bde00, 0x35060000)
    ro space 10240K,  51% used [0x38460000, 0x38993000, 0x38993000, 0x38e60000)
    rw space 12288K,  55% used [0x38e60000, 0x394fe4f8, 0x394fe600, 0x39a60000)
 

4、动态对象年龄判定

  • 为了能更好第适应不同程序的内存状况,虚拟机并不是永远地要求对象的年龄必须达到了MaxTenuringThreshold才能晋升老年代,如果在Survivor空间中相同年龄所有对象大小的总和大于Survivor空间的一半,年龄大于或等于该年龄的对象就可以直接进入老年代,无须等到MaxTenuringThreshold中要求的年龄。
  • 执行代码如下面所示,设置-XX:MaxTenuringThreshold=15,会发现运行结果中Survivor的空间占用仍然为0%,而老年代比预期增加了6%,也就是说,allocation1、allocation2对象都直接进入了老年代,而没有等到15岁的临界年龄。因为这两个对象加起来已经到达了512KB,并且它们是同年的,满足同年对象达到Survivor空间的一半规则。
package com.tenuringdistribution;

/**
 * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 
 * -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution
 * @author Administrator
 *
 */
public class TenuringThreshold2 {
	private static final int _1MB = 1024 * 1024;
	public static void testTenuringThreshold() {
		byte[] allocation1, allocation2, allocation3, allocation4;
		allocation1 = new byte[_1MB / 4];
		allocation2 = new byte[_1MB / 4];
		allocation3 = new byte[4 * _1MB];
		allocation4 = new byte[4 * _1MB];
		allocation4 = null;
		allocation4 = new byte[4 * _1MB];
	}
	
	public static void main(String[] args) {
		testTenuringThreshold();
	}
}

运行结果:

[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 15)
– age   1:     677880 bytes,     677880 total
: 4951K->661K(9216K), 0.0062589 secs] 4951K->4758K(19456K), 0.0062942 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
– age   1:        136 bytes,        136 total
: 4921K->0K(9216K), 0.0013333 secs] 9017K->4757K(19456K), 0.0013714 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 9216K, used 4260K [0x33060000, 0x33a60000, 0x33a60000)
  eden space 8192K,  52% used [0x33060000, 0x33488fe0, 0x33860000)
  from space 1024K,   0% used [0x33860000, 0x33860088, 0x33960000)
  to   space 1024K,   0% used [0x33960000, 0x33960000, 0x33a60000)
 tenured generation   total 10240K, used 4757K [0x33a60000, 0x34460000, 0x34460000)
   the space 10240K,  46% used [0x33a60000, 0x33f05738, 0x33f05800, 0x34460000)
 compacting perm gen  total 12288K, used 375K [0x34460000, 0x35060000, 0x38460000)
   the space 12288K,   3% used [0x34460000, 0x344bdcc0, 0x344bde00, 0x35060000)
    ro space 10240K,  51% used [0x38460000, 0x38993000, 0x38993000, 0x38e60000)
    rw space 12288K,  55% used [0x38e60000, 0x394fe4f8, 0x394fe600, 0x39a60000)
 

5、空间分配担保

  • 在发生Minor GC之前,虚拟机会先检查老年代最大可用的连续空间是否大于新生代所有对象总空间,如果这个条件成立,那么Minor GC可以确保是安全的。如果不成立,则虚拟机会查看HandlePromotionFailure设置值是否允许担保失败。如果允许,那么会继续查看老年代最大可用的连续空间是否大于历次晋升到老年代对象的平均大小,如果大于,将尝试着进行一次Minor GC,尽管这次Minor GC是有风险的;如果小于,或者HandlePromotionFailure设置不允许冒险,那这时也要改为进行一次Full GC。
  • JDK 6 Update 24之后的规则变为只要老年代的连续空间大于新生代对象总大小或者历次晋升的平均大小就会进行Minor GC,否则将进行Full GC。

 

    原文作者:java虚拟机
    原文地址: https://blog.csdn.net/marshalljian/article/details/81711295
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞