新生代与老年代垃圾收集器实现详解
默认情况下:
虚拟机中新生代和老年代所采用的垃圾收集器为:【在jdk1.8中,并不能保证其他的】
PSyoungGen:Parallel Scavenge(新生代垃圾收集器);
ParOldGen:Parallel Old(老年代垃圾收集器)
★★★
当新生代无法容纳下将要创建的对象的话,那么这个对象将在老年代产生分配。
实例:
public class MyTest1 {
public static void main(String[] args) {
int size = 1024 * 1024;
byte[] myAlloc1 = new byte[2 * size];
byte[] myAlloc2 = new byte[2 * size];
byte[] myAlloc3 = new byte[3 * size];
byte[] myAlloc4 = new byte[3 * size];
System.out.println("hello world");
}
}
[GC (Allocation Failure) [PSYoungGen: 7133K->992K(9216K)] 7133K->3419K(19456K), 0.0050842 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 6434K->960K(9216K)] 8861K->8507K(19456K), 0.0127432 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
//对Full GC进行分析:
//在实际的业务开发中,应尽量避免Full GC
//ParOldGen: 7546K->8112K(10240K) ,可以看到,老年代的数量变多了,这种属于正常现象。
[Full GC (Ergonomics) [PSYoungGen: 960K->0K(9216K)] [ParOldGen: 7546K->8112K(10240K)] 8507K->8112K(19456K), [Metaspace: 3311K->3311K(1056768K)], 0.0150647 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
hello world
Heap
PSYoungGen total 9216K, used 3361K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 8192K, 41% used [0x00000000ff600000,0x00000000ff9487c0,0x00000000ffe00000)
from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
to space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
ParOldGen total 10240K, used 8112K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 79% used [0x00000000fec00000,0x00000000ff3ec048,0x00000000ff600000)
Metaspace used 3320K, capacity 4568K, committed 4864K, reserved 1056768K
class space used 361K, capacity 392K, committed 512K, reserved 1048576K