Java垃圾回收ygc代码模拟
作者:互联网
1、先来看看一个成功的按照预想进行了一次ygc的例子
/** * ygc测试 * -Xms10m -Xmx10m -Xmn5m -XX:+UseParallelGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log 设置10m堆大小,年轻代和老年代各分5m,年轻代里伊甸区4m、两个幸存者区都是0.5m * */ public class TestYoungGC2 { private static int _1MB = 1024*1024; public static void main(String[] args) { List cache = new ArrayList<byte[]>(); //只能循环三次新增3M对象、到第4次就会发生ygc,因为eden本身不是完全4M可用的 for (int i = 0; i < 4; i++){ System.out.println("循环" + (i+1) + "开始"); cache.add(new byte[_1MB]); cache.remove(0); System.out.println("循环" + (i+1) + "结束"); } System.out.println("此时ygc回收了3M垃圾剩余1M对象、但这1M对象也失去了引用,下一次ygc将被回收"); cache.add(new byte[2*_1MB]); System.out.println("此时又新分配2M对象到eden, 最后新生代3M+, 老年代接近0M"); } }
输出:
循环1开始
循环1结束
循环2开始
循环2结束
循环3开始
循环3结束
循环4开始
0.158: [GC (Allocation Failure) [PSYoungGen: 4046K->496K(4608K)] 4046K->644K(9728K), 0.0009270 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
循环4结束
此时ygc回收了3M垃圾剩余1M对象、但这1M对象也失去了引用,下一次ygc将被回收
此时又新分配2M对象到eden, 最后新生代3M+, 老年代接近0M
Heap
PSYoungGen total 4608K, used 3688K [0x00000000ffb00000, 0x0000000100000000, 0x0000000100000000)
eden space 4096K, 77% used [0x00000000ffb00000,0x00000000ffe1e368,0x00000000fff00000)
from space 512K, 96% used [0x00000000fff00000,0x00000000fff7c020,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 5120K, used 148K [0x00000000ff600000, 0x00000000ffb00000, 0x00000000ffb00000)
object space 5120K, 2% used [0x00000000ff600000,0x00000000ff625010,0x00000000ffb00000)
Metaspace used 2579K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 286K, capacity 386K, committed 512K, reserved 1048576K
标签:0x00000000ffb00000,used,Java,space,循环,垃圾,3M,ygc 来源: https://www.cnblogs.com/lyhero11/p/13944971.html