記憶體分配與回收策略-例項
阿新 • • 發佈:2020-12-02
程式碼1-1 新生代 Minor GC
一、新生代 Minor GC
public class One { private static final int _1MB = 1024 * 1024; /** * VM引數:-XX:+UseSerialGC -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 */ 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]; // 出現一次Minor GC } public static void main(String[] args) { testAllocation(); } }
[GC (Allocation Failure) [DefNew: 6304K->639K(9216K), 0.0028333 secs] 6304K->4735K(19456K), 0.0028708 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap def new generation total 9216K, used 7104K[0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) eden space 8192K, 78% used [0x00000000fec00000, 0x00000000ff250698, 0x00000000ff400000) from space 1024K, 62% used [0x00000000ff500000, 0x00000000ff59fd38, 0x00000000ff600000) to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000) tenured generation total 10240K, used 4096K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000) the space 10240K, 40% used [0x00000000ff600000, 0x00000000ffa00020, 0x00000000ffa00200, 0x0000000100000000) Metaspace used 3486K, capacity 4496K, committed 4864K, reserved 1056768K class space used 386K, capacity 388K, committed 512K, reserved 1048576K
這裡有點問題感覺,不知道4MB的allocation4為什麼被分配到了老年區。jdk版本是1.8的。
二、大物件直接進入老年代
public class Two { private static final int _1MB = 1024 * 1024; /* * VM引數: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC -XX:PretenureSizeThreshold=3145728 */ 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 2372K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) eden space 8192K, 28% used [0x00000000fec00000, 0x00000000fee51270, 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 3466K, capacity 4496K, committed 4864K, reserved 1056768K class space used 383K, capacity 388K, committed 512K, reserved 1048576K