java面试-内存分配与回收策略

1、对象优先在Eden分配
-Xms20M -Xmx20M java堆的大小20M
-Xmn10M 新生代10M 老年代10M
-XX:SurvivorRatio=8 新生代Eden与一个Survivor区的空间比例8:1
/**
 * -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
 */
public class TestAllocation {

    private static final int _1MB = 1024 * 1024;

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

    public static void main(String[] args) {
        testAllocation();
    }
}

分配allocation4对象的语句时会发生一次minor GC

GC期间虚拟机发现3个2MB大小的对象无法放入survivor空间,只好通过分配担保机制提前转移到老年代。

2、大对象直接进入老年代
-XX:PretenureSizeThreshold=3145728 大于这个设置值的对象直接在老年代分配
/**
 * -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728
 */
public class TestPretenureSizeThreshold {
    private static final int _1MB = 1024 * 1024;

    public static void testPretenureSizeThreshold() {
        byte[] allocation;
        allocation = new byte[4 * _1MB];  //直接分配在老年代中
    }

    public static void main(String[] args) {
        testPretenureSizeThreshold();
    }
}  
目的:避免在Eden区和两个Survivor区直接发生大量的内存复制
3、长期存活的对象将进入老年代
-XX:MaxTenuringThreshold=1 对象晋升老年代的阈值(默认为1 /**
 *-Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1
 */
public class TestTenuringThreshold {
    private static final int _1MB = 1024 * 1024;

    public static void testTenuringThreshold() {
        byte[] allocation1, allocation2, allocation3;
        allocation1 = new byte[_1MB / 4]; //256k内存,survivor空间可以容纳
        allocation2 = new byte[4 * _1MB];
        allocation3 = new byte[4 * _1MB];
        allocation3 = null;
        allocation3 = new byte[4 * _1MB];
    }

    public static void main(String[] args) {
        testTenuringThreshold();
    }
}
 
4、动态对象年龄判定
如果在Survivor空间中相同年龄所有对象大小的总和大于Survivor空间的一半,年龄大于或等于该年龄的对象就可以直接进入老年代,无须等到MaxTenuringThreshold中要求的年龄
5、空间分配担保
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/wjh123/p/11408411.html