JVM内存分配与回收策略

对象优先在Eden分配

  SurvivorRatio:将新生代中Eden区域与Survivor区域的容量比值,默认为8,代表Eden:Survivor=8:1

 1 public class TestObjectForEden {
 2 
 3     private static final int _1MB=1024*1024;
 4 
 5     public static void testAllocation(){
 6         byte[] allocation1,allocation2,allocation3,allocation4;
 7         allocation1=new byte[2*_1MB];
 8         allocation2=new byte[2*_1MB];
 9         allocation3=new byte[2*_1MB];
10         allocation4=new byte[2*_1MB];//出现一次Minor GC
11     }
12 
13     public static void main(String[] args) {
14         testAllocation();
15     }
16 
17 }

运行结果为:

  虚拟机参数为:-verbose:gc -Xms20M -Xmx20M -Xmn20M -XX:+PrintGCDetails -XX:SurvivorRatio=8

  Heap
  def new generation total 18432K, used 12838K [0x03c00000, 0x04ff0000, 0x04ff0000)
  eden space 16448K, 78% used [0x03c00000, 0x04889b68, 0x04c10000)
  from space 1984K, 0% used [0x04c10000, 0x04c10000, 0x04e00000)
  to space 1984K, 0% used [0x04e00000, 0x04e00000, 0x04ff0000)
  tenured generation total 64K, used 0K [0x04ff0000, 0x05000000, 0x05000000)
  the space 64K, 0% used [0x04ff0000, 0x04ff0000, 0x04ff0200, 0x05000000)
  Metaspace used 2109K, capacity 2312K, committed 2368K, reserved 4480K

 

  虚拟机参数为:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8

  [GC (Allocation Failure) [DefNew: 8093K->1016K(9216K), 0.0158618 secs] 8093K->5135K(19456K), 0.0289222 secs] [Times: user=0.00 sys=0.01, real=0.03 secs]
  Disconnected from the target VM, address: '127.0.0.1:57678', transport: 'socket'
  Heap
  def new generation total 9216K, used 5354K [0x04000000, 0x04a00000, 0x04a00000)
  eden space 8192K, 52% used [0x04000000, 0x0443ca98, 0x04800000)
  from space 1024K, 99% used [0x04900000, 0x049fe0d8, 0x04a00000)
  to space 1024K, 0% used [0x04800000, 0x04800000, 0x04900000)
  tenured generation total 10240K, used 4119K [0x04a00000, 0x05400000, 0x05400000)
  the space 10240K, 40% used [0x04a00000, 0x04e05cb0, 0x04e05e00, 0x05400000)
  Metaspace used 2109K, capacity 2312K, committed 2368K, reserved 4480K

  在通过运行时通过设置虚拟机参数-Xms20M -Xmx20M -Xmn10M限制Java堆大小为20MB,不可扩展,10MB分配给新生代,剩下10MB分给老年代。

 1 public class TestObjectForEden {
 2 
 3     private static final int _1MB=1024*1024;
 4 
 5     public static void testAllocation(){
 6         byte[] allocation1,allocation2,allocation3,allocation4;
 7         allocation1=new byte[2*_1MB];
 8         allocation2=new byte[2*_1MB];
 9         allocation3=new byte[2*_1MB];
10         allocation4=new byte[4*_1MB];//出现一次Minor GC
11     }
12 
13     public static void main(String[] args) {
14         testAllocation();
15     }
16 
17 }

  [GC (Allocation Failure) [DefNew: 8093K->1016K(9216K), 0.0168326 secs] 8093K->5135K(19456K), 0.0169262 secs] [Times: user=0.00 sys=0.01, real=0.02 secs]
  Heap
  def new generation total 9216K, used 7402K [0x03e00000, 0x04800000, 0x04800000)
  eden space 8192K, 77% used [0x03e00000, 0x0443ca98, 0x04600000)
  from space 1024K, 99% used [0x04700000, 0x047fe0d8, 0x04800000)
  to space 1024K, 0% used [0x04600000, 0x04600000, 0x04700000)
  tenured generation total 10240K, used 4119K [0x04800000, 0x05200000, 0x05200000)
  the space 10240K, 40% used [0x04800000, 0x04c05cb0, 0x04c05e00, 0x05200000)
  Metaspace used 2109K, capacity 2312K, committed 2368K, reserved 4480K

 1 public class TestObjectForEden {
 2 
 3     private static final int _1MB=1024*1024;
 4 
 5     public static void testAllocation(){
 6         byte[] allocation1,allocation2,allocation3,allocation4;
 7         allocation1=new byte[2*_1MB];
 8         allocation2=new byte[2*_1MB];
 9         allocation3=new byte[2*_1MB];
10         allocation4=new byte[6*_1MB];//出现一次Minor GC
11     }
12 
13     public static void main(String[] args) {
14         testAllocation();
15     }
16 
17 }

  [GC (Allocation Failure) [DefNew: 8093K->1016K(9216K), 0.0159405 secs] 8093K->5135K(19456K), 0.0160188 secs] [Times: user=0.00 sys=0.02, real=0.02 secs] 

  [GC (Allocation Failure) [DefNew: 3064K->0K(9216K), 0.0097685 secs] 7183K->7000K(19456K), 0.0098040 secs] [Times: user=0.00 sys=0.02, real=0.01 secs]
  Heap
  def new generation total 9216K, used 6226K [0x04800000, 0x05200000, 0x05200000)
  eden space 8192K, 76% used [0x04800000, 0x04e14938, 0x05000000)
  from space 1024K, 0% used [0x05000000, 0x05000000, 0x05100000)
  to space 1024K, 0% used [0x05100000, 0x05100000, 0x05200000)
  tenured generation total 10240K, used 7000K [0x05200000, 0x05c00000, 0x05c00000)
  the space 10240K, 68% used [0x05200000, 0x058d6088, 0x058d6200, 0x05c00000)
  Metaspace used 2109K, capacity 2312K, committed 2368K, reserved 4480K

posted @ 2019-05-27 23:24  在旅途上追忆寻梦  阅读(259)  评论(0编辑  收藏  举报