java interview - memory allocation and recovery strategies

1, the object priority assignment in Eden
-Xms20M -Xmx20M java heap size 20M
-Xmn10M new generation 10M 10M years old
-XX: SurvivorRatio = Eden space ratio of 8 new generation area and a 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();
    }
}

A minor GC occurs when the statement allocation4 object allocation

During GC virtual machine found three 2MB size of the object does not fit survivor space, had transferred in advance to guarantee mechanisms by assigning years old.

2, large objects directly into the old year
-XX: PretenureSizeThreshold = 3145728 larger than the set value of objects directly allocated in the old years
/**
 * -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();
    }
}  
Objective: Avoid Eden area and two Survivor areas a lot of memory replication occurs directly
3, long-term survival of the object will enter the old year
-XX: MaxTenuringThreshold = 1 target promotion threshold of old age (the default is 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, dynamic target age determination
If the sum of the same age Survivor space is larger than half the size of all the objects in space Survivor, age greater than or equal to the object that age you can go directly years old, no need to wait until the age required in MaxTenuringThreshold
5, space allocation guarantees
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/wjh123/p/11408411.html