JVM内存分配与回收策略

JVM内存分配与回收策略

1对象优先在Eden分配

//-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails             
    //-XX:+UseSerialGC 
    public static void testAllocation(){

        byte[] allocation1;
        byte[] allocation2;
        @SuppressWarnings("unused")
        byte[] allocation3;
        byte[] allocation4;

        allocation1=new byte[2*_1MB];
        allocation2=new byte[2*_1MB];
        allocation3=new byte[2*_1MB];
        allocation4=new byte[4*_1MB];//出现一次Minor GC;

    }

JVM内存分配与回收策略_第1张图片

2大对象直接进入老年代

//-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails            
    //-XX:+UseSerialGC -XX:PretenureSizeThreshold=3145728
    public static void tesdPretenureSizeThreshold(){
        byte[] allocation;
        allocation=new byte[3*_1MB];

    }

JVM内存分配与回收策略_第2张图片

3长期存活的对象将进入老年代

3.1 -XX:MaxTenuringThreshold=1

//-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails            
    //-XX:+UseSerialGC  -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution
    public static void testTenuringThreshold(){
        byte[] allocation1;
        byte[] allocation2;
        byte[] allocation3;     

        allocation1=new byte[_1MB/4];
        allocation2=new byte[4*_1MB];
        allocation3=new byte[4*_1MB];
        allocation3=null;
        allocation3=new byte[4*_1MB];


    }

JVM内存分配与回收策略_第3张图片
3.2 -XX:MaxTenuringThreshold=15

//-verbose:gc -Xms20M -Xmx20M -Xmn10M -//XX:SurvivorRatio=8 -XX:+PrintGCDetails            
//-XX:+UseSerialGC  -XX:MaxTenuringThreshold=15 
//-XX:+PrintTenuringDistribution
    public static void testTenuringThreshold(){
        byte[] allocation1;
        byte[] allocation2;
        byte[] allocation3;     

        allocation1=new byte[_1MB/4];
        allocation2=new byte[4*_1MB];
        allocation3=new byte[4*_1MB];
        allocation3=null;
        allocation3=new byte[4*_1MB];


    }

JVM内存分配与回收策略_第4张图片

4动态对象年龄判定

//-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails            
//-XX:+UseSerialGC  -XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution
    public static void testTenuringThreshold(){
        byte[] allocation1;
        byte[] allocation2;
        byte[] allocation3;     

        allocation1=new byte[_1MB/4];
        allocation2=new byte[4*_1MB];
        allocation3=new byte[4*_1MB];
        allocation3=null;
        allocation3=new byte[4*_1MB];


    }

JVM内存分配与回收策略_第5张图片

5空间分配担保

//-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails            
//-XX:+UseSerialGC   -XX:-HandlePromotionFailure=false
    public static void testHandlePromotion(){
        byte[] allocation1;
        byte[] allocation2;
        byte[] allocation3;
        byte[] allocation4;
        byte[] allocation5;
        byte[] allocation6;
        byte[] allocation7;

        allocation1=new byte[2*_1MB];
        allocation2=new byte[2*_1MB];
        allocation3=new byte[2*_1MB];
        allocation1=null;
        allocation4=new byte[2*_1MB];
        allocation5=new byte[2*_1MB];
        allocation6=new byte[2*_1MB];
        allocation4=null;
        allocation5=null;
        allocation6=null;

        allocation7=new byte[2*_1MB];
    }

你可能感兴趣的:(JVM)