This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

preface

GC common related parameters and heap memory usage – nuggets (juejin. Cn)

This article covers common parameters for GC and viewing heap memory

First we create the DemoTest class to add the main method and add the JVM parameters: -xms20m -XMx20m -xmn10m -xx:+ UserSerialGC -xx:+PrintGCDetails -verbose: GC

Then run the empty main method as follows (see the previous article for details)

Analysis of the GC

Create an ArrayList in the main method with the array byte[] and make it a local variable on the stack frame referencing an object in the heap. The object will not be recycled at runtime due to the GC Root reference.

Then add a 5M byte array to the object. According to the figure above, we can see that 45 percent of Eden’s byte array has been occupied. Then add 5M to Eden’s byte array

    private static final int SEVEN_MB = 7 * 1024 * 1024;
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Byte[SEVEN_MB]);
    }
Copy the code

Results after operation:

If the GC occurred in the old era, the first prompt is Full GC, and the GC occurred in the new generation is called GC

The two values behind DefNew represent the size before the collection and the size after the collection, the parentheses represent the total size of the new generation, and the values following represent the time of the new generation garbage collection execution.

The information after the execution time is the total heap usage before and after execution, with the total heap size in parentheses, followed by the total heap reclamation time

Then the memory area is changed, Eden is occupied by 5%, that is, because the surviving From and surviving To memory will be swapped during GC, so the surviving memory space is occupied by 65%

Add the byte array to the array and add 1M:

    private static final int SEVEN_MB = 7 * 1024 * 1024;
    private static final int _1MB = 1 * 1024 * 1024;
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Byte[SEVEN_MB]);
        list.add(new Byte[_1MB]);
    }
Copy the code

Run again to see GC information:

Two garbage collections have been triggered here, and 76 percent of the old space has been taken up.

Let’s go ahead and add the byte array, and we’ll add another 15M here

private static final int SEVEN_MB = 7 * 1024 * 1024;
private static final int _15MB = 15 * 1024 * 1024;
private static final int _1MB = 1 * 1024 * 1024;
public static void main(String[] args) {
    ArrayList list = new ArrayList();
    list.add(new Byte[SEVEN_MB]);
    list.add(new Byte[_1MB]);
    list.add(new Byte[_15MB]);
}
Copy the code

Adding another 15M exceeded the maximum heap size we set, so a heap overflow error message was thrown and a Fubll GC was performed.