JVM_05 runtime data area 2-heap_1

1. Subdivided memory structure of the heap

  • Before JDK 7: Newborn area + senior care area + permanent area

    • Young Generation Space: divided into Eden area and Survior area Young/New
    • Tenure generation Space: Old/Tenure
    • Permanent Space: Perm

Insert picture description here

  • After JDK 8: Newborn area + senior care area + metaspace

    • Young Generation Space: divided into Eden area and Survior area Young/New
    • Tenure generation Space: Old/Tenure
    • Meta Space: Meta

Insert picture description here

2. Set the heap memory size and OOM

Insert picture description here

public class HeapSpaceInitial {
    public static void main(String[] args) {

        //返回Java虚拟机中的堆内存总量
        long initialMemory = Runtime.getRuntime().totalMemory() / 1024 / 1024;
        //返回Java虚拟机试图使用的最大堆内存量
        long maxMemory = Runtime.getRuntime().maxMemory() / 1024 / 1024;

        System.out.println("-Xms : " + initialMemory + "M");//-Xms : 245M
        System.out.println("-Xmx : " + maxMemory + "M");//-Xmx : 3641M

        System.out.println("系统内存大小为:" + initialMemory * 64.0 / 1024 + "G");//系统内存大小为:15.3125G
        System.out.println("系统内存大小为:" + maxMemory * 4.0 / 1024 + "G");//系统内存大小为:14.22265625G

        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Set the heap size to 600m, and the printed result is 575m. This is because the survivor area S0 and S1 each occupy 25m, but one of them is always empty. The objects stored are the Eden area and a survivor area.
Insert picture description here

3. The young generation and the old generation

Insert picture description here

Configure the proportion of the young generation and the old generation in the heap structure

  • Default -XX: NewRatio=2, which means that the new generation accounts for 1, the old generation accounts for 2, and the new generation accounts for 1/3 of the entire heap

  • You can modify -XX:NewRatio=4, which means that the new generation occupies 1, the old generation occupies 4, and the new generation occupies 1/5 of the entire heap

  • In hotSpot, the default ratio of the Eden space and the other two Survivor spaces is 8:1:1 (6:1:1 during testing). Developers can adjust the space ratio through the option -XX:SurvivorRatio, such as -XX:SurvivorRatio=8

  • Almost all Java objects are new in the Eden area

  • The vast majority of Java objects are destroyed in the new generation (IBM's special research shows that 80% of the objects in the new generation are "dying")

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/114803872