Code Demonstration GC Recycling

JVM configuration

-XX:NewSize=5m initial new generation size
-XX:MaxNewSize=5m maximum new generation size
-XX:InitialHeapSize=10m initial heap size equal to Xms
-XX:MaxHeapSize=10m maximum heap size equal to Xmx
-XX:SurvivorRatio=8 Eden area accounts for 80%
-XX:PretenureSizeThreshold=10m Large object threshold
-XX:+UseParNewGC The new generation uses ParNew
-XX:+UseConcMarkSweepGC The old generation uses CMS
-XX:+PrintGCDetils: Print detailed gc logs
-XX:+PrintGCTimeStamps: This The parameter can print out the time of each GC occurrence
-Xloggc:gc.log: This parameter can be set to write the gc log to a disk file

the code

public class ParNew {
    
    

    public static void main(String[] args) {
    
    
        System.out.println("main方法压入栈帧-----");
        int size = 1024 * 1024;
        System.out.println("在main的栈帧内创建一个arrarray11变量,并在eden区开辟一块1m的内存,变量arr1指向这块你内存");
        byte[] array1 = new byte[size];
        System.out.println("在堆内存的Eden区中创建第二个数组,并且让局部变量指向第二个数组,第一个数组就没人引用了,此时第一个数组就成了没人引用的“垃圾对象”");
        array1 = new byte[size];
        System.out.println("在堆内存的Eden区内创建了第三个数组,同时让array1变量指向了第三个数组,此时前面两个数组都没有人引用了,就都成了垃圾对象");
        array1 = new byte[size];
        System.out.println("array1这个变量什么都不指向了,此时会导致之前创建的3个数组全部变成垃圾对象");
        array1 = null;
        System.out.println("此时会分配一个2MB大小的数组,尝试放入Eden区中,但是Edeb区只剩1m了,所以会触发Minor GC");
        byte[] arr2 = new byte[2 * size];

    }
}

code analysis

  1. byte[] arr1 = new byte[size];Once this line of code is run, a 1MB object will be placed in the Eden area of ​​the JVM, and a stack frame of the main() method will be pushed into the virtual machine stack of the main thread. Inside, there will be an "array1" variable, which points to the 1MB array in the Eden area of ​​the heap memory, as shown in the figure below.

insert image description here

  1. array1 = new byte[size];At this time, the second array will be created in the Eden area of ​​the heap memory, and the local variable will point to the second array, and then the first array will be unreferenced, and the first array will become unreferenced. "Garbage object", as shown in the figure below.
    insert image description here

  2. array1 = new byte[size];This line of code creates a third array in the Eden area of ​​the heap memory, and at the same time makes the array1 variable point to the third array. At this time, no one references the first two arrays, and they become garbage objects, as shown in the figure below Show.
    insert image description here

  3. array1 = null;Once this line of code is executed, the variable array1 will no longer point to anything. At this time, all the three previously created arrays will become garbage objects, as shown in the figure below.
    insert image description here

  4. byte[] arr2 = new byte[2 * size];At this time, a 2MB array will be allocated, try to put it in the Eden area, because the Eden area is only 4MB in size, and three 1MB arrays have been placed in it, so the remaining space is only 1MB, at this time you put a The 2MB array cannot fit, so the Young GC of the young generation will be triggered at this time.

Guess you like

Origin blog.csdn.net/u010859650/article/details/127975782