天天看點

java 大對象老年代_JDK1.8 大對象直接進入老年代(參考深入了解JAVA虛拟機)

jvm參數

-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728

代碼

package com.memory.gc;

public class PretenureSizeThreshold {

private static final int _1MB = 1024 * 1024;

public static void testPretenureSizeThreshold(){

byte[] allocation;

allocation = new byte[5 * _1MB];

}

public static void main(String[] args) {

PretenureSizeThreshold.testPretenureSizeThreshold();

}

}

結果

Heap

PSYoungGen total 9216K, used 7612K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)

eden space 8192K, 92% used [0x00000000ff600000,0x00000000ffd6f080,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 3472K, capacity 4496K, committed 4864K, reserved 1056768K

class space used 381K, capacity 388K, committed 512K, reserved 1048576K

明顯看出與原書不同,原因是PretenureSizeThreshold參數隻對Serial和ParNew兩款收集器有效,Parallel Scavenge收集器不認識這個參數,Parallel Scavenge收集器一般并不需要設定。如果遇到必須使用此參數的場合,可以考慮ParNew加CMS的收集器組合。

若想實作書中的效果修改jvm參數

-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728 -XX:+UseParNewGC

代碼

package com.memory.gc;

public class PretenureSizeThreshold {

private static final int _1MB = 1024 * 1024;

public static void testPretenureSizeThreshold(){

byte[] allocation;

allocation = new byte[5 * _1MB];

}

public static void main(String[] args) {

PretenureSizeThreshold.testPretenureSizeThreshold();

}

}

結果

Heap

par new generation total 9216K, used 2492K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)

eden space 8192K, 30% used [0x00000000fec00000, 0x00000000fee6f070, 0x00000000ff400000)

from space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)

to space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)

tenured generation total 10240K, used 5120K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)

the space 10240K, 50% used [0x00000000ff600000, 0x00000000ffb00010, 0x00000000ffb00200, 0x0000000100000000)

Metaspace used 3407K, capacity 4496K, committed 4864K, reserved 1056768K

class space used 374K, capacity 388K, committed 512K, reserved 1048576K