jvm内存分配与收回策略

【README】基础知识
1、    Minor GC/新生代GC:指发生在新生代的垃圾收集动作,因为java对象大多都具备朝生夕灭的特性,所以minor gc比较频繁,一般回收速度也比较快;
2、    Major GC/Full GC/老年代GC:发生在老年代的gc,出现了major gc, 经常会伴着出现minor gc(并非绝对)。Major gc的速度一般会比minor gc慢10倍以上; 
 

【1】对象优先在eden区域分配

/**
 * 对象优先在eden区域分配
 */
public class Page93 {
	private static final int _1MB = 1024 * 1024;
	
	public static void main(String[] args) {
		minorGC(); 
	}
	/**
	 * vm params: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
	 */
	private static void minorGC() {
		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]; 
	}
}
// gc日志和内存区域使用情况 
Heap
 PSYoungGen      total 9216K, used 7291K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 89% used [0x00000000ff600000,0x00000000ffd1efb0,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
 Metaspace       used 2624K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 279K, capacity 386K, committed 512K, reserved 1048576K

1.1、jvm参数分析:

-Xms20M设置java堆容量最小值;

-Xmx20M设置 java堆容量最大值;

-Xmn10M设置java堆新生代容量;(所以java堆老年代容量为20M-10M=10M)

-XX:+PrintGCDetials设置在发生gc时打印内存回收日志,并且在进程退出时输出当前的内存各区域分配情况;

-XX:SurvivorRatio设置Eden:Suivivor: Suivivor = 8:1:1

所以java堆的新生代的可用容量为9M=Eden区+1个Survivor区的总容量;

1.2、PSYoungGen表示其堆新生代的垃圾收集器是Parallel Scavenge(吞吐量+自适应);

总容量=0x0000000100000000-0x00000000ff600000=0xa00000字节=10MB;

已使用容量=7298K;

Eden区容量=8M,已使用量=0x00000000ffd20b48-0x00000000ff600000=0x0000000000720b48(16)=7M+131K

Survivor区容量=1M,使用量=0

Survivor区容量=1M,使用量=0

1.3、ParOldGen标识其堆老年代的垃圾收集器是 Parallel Old(吞吐量优先收集器组合PO+PS);

总容量=0x00000000ff600000-0x00000000fec00000=0xa00000字节=10MB;

已使用量=4M;

 

【2】大对象直接进入老年代

/**
 * 大对象直接进入老年代
 */
public class Page94 {
	
	private static final int _1MB = 1024 * 1024;
	
	public static void main(String[] args) {
		testPretenureSizeThreshold(); 
	}
	/**
	 * vm params: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728(3M)
	 */
	private static void testPretenureSizeThreshold() {
		byte[] allocation;
		allocation = new byte[4 * _1MB]; 
	}
}
// gc日志和内存区域使用情况 
Heap
 PSYoungGen      total 9216K, used 5243K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 64% used [0x00000000ff600000,0x00000000ffb1ef90,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 0K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ff600000)
 Metaspace       used 2624K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 279K, capacity 386K, committed 512K, reserved 1048576K

【3】长期存活对象将进入老年代

/**
 * 长期存活的对象进入老年代 
 */
public class Page95 {
	
	private static final int _1MB = 1024 * 1024;
	
	public static void main(String[] args) {
		testTenuringThreshold(); 
	}
	/**
	 * vm params: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 
	 * -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution 
	 */
	private 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]; 
	}
}
// gc日志和内存区域使用情况
Heap
 PSYoungGen      total 9216K, used 5499K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 67% used [0x00000000ff600000,0x00000000ffb5efa0,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 8192K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 80% used [0x00000000fec00000,0x00000000ff400020,0x00000000ff600000)
 Metaspace       used 2624K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 279K, capacity 386K, committed 512K, reserved 1048576K

【4】动态对象年龄判定

/**
 * 动态对象年龄判定
 */
public class Page97 {
	private static final int _1MB = 1024 * 1024;

	public static void main(String[] args) {
		testTenuringThreshold();
	}
	/**
	 * vm params: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails
	 * -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=15
	 * -XX:+PrintTenuringDistribution
	 */
	private static void testTenuringThreshold() {
		byte[] allocation1, allocation2, allocation3, allocation4;
		allocation1 = new byte[_1MB / 4];
		/* allocation1+allocation2 大于survivor空间一半 */
		allocation2 = new byte[_1MB / 4];
		allocation3 = new byte[4 * _1MB];
		allocation4 = new byte[4 * _1MB];
		allocation4 = null;
		allocation4 = new byte[4 * _1MB];
		allocation4 = new byte[4 * _1MB];
		allocation4 = new byte[4 * _1MB];
	}
}
// gc日志和内存区域使用情况
[GC (Allocation Failure) --[PSYoungGen: 5591K->5591K(9216K)] 13783K->14047K(19456K), 0.0013548 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 5591K->0K(9216K)] [ParOldGen: 8456K->9216K(10240K)] 14047K->9216K(19456K), [Metaspace: 2617K->2617K(1056768K)], 0.0061721 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) --[PSYoungGen: 4096K->4096K(9216K)] 13312K->13312K(19456K), 0.0006086 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 4096K->0K(9216K)] [ParOldGen: 9216K->9215K(10240K)] 13312K->9215K(19456K), [Metaspace: 2618K->2618K(1056768K)], 0.0050350 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 9216K, used 4178K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 51% used [0x00000000ff600000,0x00000000ffa14930,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 9215K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 89% used [0x00000000fec00000,0x00000000ff4ffcc0,0x00000000ff600000)
 Metaspace       used 2624K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 279K, capacity 386K, committed 512K, reserved 1048576K

【5】空间分配担保

/**
 * 空间分配担保
 */
public class Page99 {
	private static final int _1MB = 1024 * 1024;

	public static void main(String[] args) {
		testHandlePromotion();
	}
	/** 
	 * vm params: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails
	 * -XX:SurvivorRatio=8 -XX:-HandlePromotionFailure(HandlePromotionFailure这个参数不起作用)
	 */
	private static void testHandlePromotion() {
		byte[] allocation1, allocation2, allocation3, allocation4, allocation5, allocation6, allocation7;
		allocation1 = new byte[2 * _1MB];
		allocation2 = new byte[2 * _1MB];
		allocation3 = new byte[2 * _1MB];
		allocation1 = null;
		allocation4 = new byte[2 * _1MB];
		allocation5 = new byte[2 * _1MB];
		allocation6 = new byte[2 * _1MB];
		allocation4 = null;
		allocation5 = null;
		allocation6 = null;
		allocation7 = new byte[2 * _1MB];
	}
}
// gc日志和内存区域使用情况 
[GC (Allocation Failure) [PSYoungGen: 7127K->728K(9216K)] 7127K->4832K(19456K), 0.0027073 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 7032K->584K(9216K)] 11136K->4688K(19456K), 0.0009033 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 9216K, used 2796K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 27% used [0x00000000ff600000,0x00000000ff8290e0,0x00000000ffe00000)
  from space 1024K, 57% used [0x00000000fff00000,0x00000000fff92020,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 4104K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 40% used [0x00000000fec00000,0x00000000ff002020,0x00000000ff600000)
 Metaspace       used 2624K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 279K, capacity 386K, committed 512K, reserved 1048576K

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值