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;

    }

这里写图片描述

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];

    }

这里写图片描述

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];


    }

这里写图片描述
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];


    }

这里写图片描述

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];


    }

这里写图片描述

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];
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值