“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

Detailed GC memory allocation and reclamation strategies for Java objects, as well as common GC logging parameters.

1 Viewing GC logs

We’ve learned a lot about the garbage collector, but if garbage collection is frequent or takes too much CPU time, it’s a concern. In this case, some trace parameters are needed to further identify the efficiency and effectiveness of the garbage collector. We need to learn how to output and view GC logs. This article is based on JDK1.8+IDEA to help you output and view GC logs.

1.1 Common PARAMETERS for GC Logs

parameter function
-XX:+PrintGC Output simple GC logs
-XX:+PrintGCDetails Verbose GC logs are printed, and stack usage information is printed before the virtual machine exits.
-XX:+PrintHeapAtGC Stack information is printed before and after each GC
-XX:+PrintGCTimeStamps Outputs the GC timestamp (in the form of baseline time), the time offset since JVM startup.
-XX:+PrintGCDateStamps Output GC timestamp (in the form of date, e.g. 2020-04-02T10:09:01.045+0800)
-Xloggc:./gc.log Output path of log files
-XX:SurvivorRatio=8 The ratio of Eden to Survivor is defined as 8:1 in the young generation
-Xmx Set the maximum amount of memory that can be occupied during the running of the program
-Xms Set the size of memory used when the program is started
-Xmn Set the memory size occupied by the young generation
-XX:MaxTenuringThreshold=N When the object age reaches N, the next GC will enter the old age, which can be 0

1.2 Output and view of GC logs

This section is covered in the examples in the Section on GC Memory allocation and reclamation Strategies for Java objects.

2 GC memory allocation and reclamation strategies for Java objects

2.1 Objects are preferentially allocated in Eden

In most cases, objects are allocated in the Eden region of the new generation.

2.1.1 test

public class GCLog {

    public static final int _1MB = 1024 * 1024;
    public static final int _1KB = 1024 ;

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

    /** * VM parameters: -Xmx64m -xMS64m -xmn10m -xx :+PrintGCTimeStamps -xx :+PrintHeapAtGC -xx :+PrintGCDetails * The object is allocated in the Eden area first */
    public static void eden(a) {
        byte[] b1, b2, b3, b4, b5;
        b1 = new byte[1 * _1MB];
        b2 = new byte[1 * _1MB];
        b3 = new byte[1 * _1MB];
        b4 = new byte[1 * _1MB];
        b5 = new byte[1* _1MB]; }}Copy the code

2.1.1.1 idea Configure VM parameters

Click on the drop-down arrow and click Edit Configurations

Click +, click Application

Click to select the corresponding Class and change the name as you wish

Configure VM parameters.

-Xmx64M -Xms64M -Xmn10M -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintGCDetails

Click Apply to finish.

2.1.2 Outputting GC Logs

Run to see the output:

Heap
 PSYoungGen      total 9216K, used 7516K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 8192K, 91% used [0x00000000ff600000.0x00000000ffd57060.0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000.0x00000000fff00000.0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000.0x00000000ffe00000.0x00000000fff00000)
 ParOldGen       total 55296K, used 0K [0x00000000fc000000.0x00000000ff600000.0x00000000ff600000)
  object space 55296K, 0% used [0x00000000fc000000.0x00000000fc000000.0x00000000ff600000)
 Metaspace       used 3412K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 374K.capacity 388K.committed 512K.reserved 1048576K

Copy the code

Reading:

The PS collector is the Parallel Scavenge collector. The YoungGen Exploiter is the YoungGen exploiter.

Total 9216K, used 7516K Total indicates the total memory available for the new generation, and userd indicates the used memory.

There are three partitions for the new generation: Eden Space, From Space, and to Space. The default size is 8:1:1.

The first Par in ParOldGen means that the garbage collector is a Parallel Old collector. The area reclaimed is OldGen, including object space, which accounts for 100% of the garbage collector.

Metaspace is a JDK1.8 metspace that contains class Spaces.

91% used is used for Eden space, and 0% is used for FROM space, to space, and object space, indicating that the objects in the case are allocated in Eden area.

2.2 Old objects will enter the old age

The virtual machine defines an object age counter for each object. If the object is still alive after Eden is born and passes the first Minor GC and can be accommodated by Survivor, it is moved to Survivor space and the object age is set to 1. Each time an object survives a Minor GC in a Survivor zone, its age increases by one year, and when it reaches a certain age (15 by default), the next GC is promoted to the old age. The maximum age of an object can be set by using -xx :MaxTenuringThreshold=N.

2.2.1 test

public class GCLog {

    private static final int _1MB = 1024 * 1024;
    public static final int _1KB = 1024;

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

    /** * VM parameters: * Objects are allocated in Eden first, and old objects enter the old age */
    private static void toOld(a) {
        for (int i = 0; i < 15; i++) {
            byte[] b2 = new byte[_1MB]; }}}Copy the code

2.2.2 Output GC Logs description

First enter the VM parameters:

-Xmx20M -Xms20M -Xmn10M -XX:+PrintGCTimeStamps -XX:SurvivorRatio=4 -XX:+PrintGC -XX:MaxTenuringThreshold=1 -XX:+PrintHeapAtGC

Indicates that on the second GC, the 1-year-old object will go straight to the old age.

The output is as follows:

{Heap before GC invocations=1 (full 0):
 PSYoungGen      total 8704K, used 6159K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 85% used [0x00000000ff600000.0x00000000ffc03f88.0x00000000ffd00000)
  from space 1536K, 0% used [0x00000000ffe80000.0x00000000ffe80000.0x0000000100000000)
  to   space 1536K, 0% used [0x00000000ffd00000.0x00000000ffd00000.0x00000000ffe80000)
 ParOldGen       total 10240K, used 0K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000.0x00000000fec00000.0x00000000ff600000)
 Metaspace       used 3465K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 380K.capacity 388K.committed 512K.reserved 1048576K0.101:GC (Allocation Failure) [PSYoungGen: 6159K- > 888K(8704K6159)]K- > 896K(18944K), 0.0010425 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap after GC invocations=1 (full 0):
 PSYoungGen      total 8704K, used 888K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 0% used [0x00000000ff600000.0x00000000ff600000.0x00000000ffd00000)
  from space 1536K, 57% used [0x00000000ffd00000.0x00000000ffdde010.0x00000000ffe80000)
  to   space 1536K, 0% used [0x00000000ffe80000.0x00000000ffe80000.0x0000000100000000)
 ParOldGen       total 10240K, used 8K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000.0x00000000fec02000.0x00000000ff600000)
 Metaspace       used 3465K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 380K.capacity 388K.committed 512K.reserved 1048576K
}
{Heap before GC invocations=2 (full 0):
 PSYoungGen      total 8704K, used 7168K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 87% used [0x00000000ff600000.0x00000000ffc22238.0x00000000ffd00000)
  from space 1536K, 57% used [0x00000000ffd00000.0x00000000ffdde010.0x00000000ffe80000)
  to   space 1536K, 0% used [0x00000000ffe80000.0x00000000ffe80000.0x0000000100000000)
 ParOldGen       total 10240K, used 8K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000.0x00000000fec02000.0x00000000ff600000)
 Metaspace       used 3465K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 380K.capacity 388K.committed 512K.reserved 1048576K0.103:GC (Allocation Failure) [PSYoungGen: 7168K- > 0K(8704K7176)]K- > 732K(18944K), 0.0008609 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap after GC invocations=2 (full 0):
 PSYoungGen      total 8704K, used 0K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 0% used [0x00000000ff600000.0x00000000ff600000.0x00000000ffd00000)
  from space 1536K, 0% used [0x00000000ffe80000.0x00000000ffe80000.0x0000000100000000)
  to   space 1536K, 0% used [0x00000000ffd00000.0x00000000ffd00000.0x00000000ffe80000)
 ParOldGen       total 10240K, used 732K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 7% used [0x00000000fec00000.0x00000000fecb7090.0x00000000ff600000)
 Metaspace       used 3465K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 380K.capacity 388K.committed 512K.reserved 1048576K
}
Heap
 PSYoungGen      total 8704K.used 5322K [0x00000000ff600000. 0x0000000100000000. 0x0000000100000000)
  eden space 7168K74%,used [0x00000000ff600000. 0x00000000ffb32b38. 0x00000000ffd00000)
  from space 1536K0%,used [0x00000000ffe80000. 0x00000000ffe80000. 0x0000000100000000)
  to   space 1536K0%,used [0x00000000ffd00000. 0x00000000ffd00000. 0x00000000ffe80000)
 ParOldGen       total 10240K.used 732K [0x00000000fec00000. 0x00000000ff600000. 0x00000000ff600000)
  object space 10240K7%,used [0x00000000fec00000. 0x00000000fecb7090. 0x00000000ff600000)
 Metaspace       used 3471K.capacity 4496K.committed 4864K.reserved 1056768K
  class space    used 381K.capacity 388K.committed 512K.reserved 1048576K

Copy the code

The above log simply prints heap memory information before and after each GC. The heap memory information is printed again at the end.

GC information interpretation:

  1. 0.101: [Allocation Failure (GC) [PSYoungGen: 6159K->888K(8704K)] 6159K->896K(18944K), 0.0010425 secs] [Times: User sys = = 0.00 0.00, real = 0.00 secs]
  2. 0.101: Relative to the time when the JVM started
  3. GC: Indicates that a garbage collection has taken place, without the Full modifier, indicating that this is a Minor GC.
  4. Allocation Failure: Indicates that the GC is caused because there is not enough space in the young generation to store new data.
  5. PSYoungGen: Indicates that the GC occurred in the younger generation and was using the Parallel Scavenge collector garbage collector.
  6. 6159K->888K(8704K) : The three parameters are: the used capacity of the memory region (here is the young generation) before GC, the used capacity of the memory region after GC, and the total capacity of the memory region.
  7. 6159K->896K(18944K) : three parameters are respectively: heap garbage collection before the use of size, heap garbage collection after the use of size, total heap size.
  8. 0.0010425 secs: Indicates the GC duration of the memory area, in seconds
  9. [Times: user=0.00 sys=0.00, real=0.00 secs] : indicates user-mode time, kernel-mode time, and total time

Check out other information:

Heap after GC invocations=1 (full 0)

From space 1536K, 57% used, indicating that more than half of survivor zones were occupied after the first GC

Heap after GC invocations=2 (full 0)

ParOldGen total 10240K, used 732K, ParOldGen total 10240K, used 732K, ParOldGen total 10240K, used 732K

Change the value of -xx :MaxTenuringThreshold=1 to 2, and the output is as follows

{Heap before GC invocations=1 (full 0):
 PSYoungGen      total 8704K, used 6159K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 85% used [0x00000000ff600000.0x00000000ffc03f88.0x00000000ffd00000)
  from space 1536K, 0% used [0x00000000ffe80000.0x00000000ffe80000.0x0000000100000000)
  to   space 1536K, 0% used [0x00000000ffd00000.0x00000000ffd00000.0x00000000ffe80000)
 ParOldGen       total 10240K, used 0K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000.0x00000000fec00000.0x00000000ff600000)
 Metaspace       used 3436K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 376K.capacity 388K.committed 512K.reserved 1048576K0.098:GC (Allocation Failure) [PSYoungGen: 6159K- > 808K(8704K6159)]K- > 816K(18944K), 0.0008813 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap after GC invocations=1 (full 0):
 PSYoungGen      total 8704K, used 808K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 0% used [0x00000000ff600000.0x00000000ff600000.0x00000000ffd00000)
  from space 1536K, 52% used [0x00000000ffd00000.0x00000000ffdca020.0x00000000ffe80000)
  to   space 1536K, 0% used [0x00000000ffe80000.0x00000000ffe80000.0x0000000100000000)
 ParOldGen       total 10240K, used 8K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000.0x00000000fec02000.0x00000000ff600000)
 Metaspace       used 3436K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 376K.capacity 388K.committed 512K.reserved 1048576K
}
{Heap before GC invocations=2 (full 0):
 PSYoungGen      total 8704K, used 7162K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 88% used [0x00000000ff600000.0x00000000ffc347e8.0x00000000ffd00000)
  from space 1536K, 52% used [0x00000000ffd00000.0x00000000ffdca020.0x00000000ffe80000)
  to   space 1536K, 0% used [0x00000000ffe80000.0x00000000ffe80000.0x0000000100000000)
 ParOldGen       total 10240K, used 8K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000.0x00000000fec02000.0x00000000ff600000)
 Metaspace       used 3445K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 377K.capacity 388K.committed 512K.reserved 1048576K0.100:GC (Allocation Failure) [PSYoungGen: 7162K- > 792K(8704K7170)]K- > 808K(18944K), 0.0011583 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap after GC invocations=2 (full 0):
 PSYoungGen      total 8704K, used 792K [0x00000000ff600000.0x0000000100000000.0x0000000100000000)
  eden space 7168K, 0% used [0x00000000ff600000.0x00000000ff600000.0x00000000ffd00000)
  from space 1536K, 51% used [0x00000000ffe80000.0x00000000fff46030.0x0000000100000000)
  to   space 1536K, 0% used [0x00000000ffd00000.0x00000000ffd00000.0x00000000ffe80000)
 ParOldGen       total 10240K, used 16K [0x00000000fec00000.0x00000000ff600000.0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000.0x00000000fec04000.0x00000000ff600000)
 Metaspace       used 3445K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 377K.capacity 388K.committed 512K.reserved 1048576K
}
Heap
 PSYoungGen      total 8704K.used 6306K [0x00000000ff600000. 0x0000000100000000. 0x0000000100000000)
  eden space 7168K76%,used [0x00000000ff600000. 0x00000000ffb62900. 0x00000000ffd00000)
  from space 1536K51%,used [0x00000000ffe80000. 0x00000000fff46030. 0x0000000100000000)
  to   space 1536K0%,used [0x00000000ffd00000. 0x00000000ffd00000. 0x00000000ffe80000)
 ParOldGen       total 10240K.used 16K [0x00000000fec00000. 0x00000000ff600000. 0x00000000ff600000)
  object space 10240K0%,used [0x00000000fec00000. 0x00000000fec04000. 0x00000000ff600000)
 Metaspace       used 3458K.capacity 4496K.committed 4864K.reserved 1056768K
  class space    used 379K.capacity 388K.committed 512K.reserved 1048576K

Copy the code

We can see that after the second GC, object space 10240K, 0% used, indicating that the 1-year-old object has not been moved to the old age.

2.2.3 Dynamically determine the age of objects

In order to better adapt to the memory conditions of different programs, the virtual machine does not always require that the object age must reach MaxTenuringThreshold to advance to the old age. If the sum of all object sizes of the same age in the Survivor space is greater than half of the Survivor space, Objects older than or equal to this age can go directly to the old age without waiting until the age specified in MaxTenuringThreshold.

2.3 Large objects directly into the old age

Large objects are those that (1) require more memory than is available, and (2) require more contiguous memory than is available. The most typical large objects are long strings and arrays (such as byte[] arrays).

Since neither Eden nor Survivor zone can accommodate large objects, they will attempt to be promoted directly to the old age.

Virtual machine provides a – XX: PretenureSizeThreshold parameters, make more than the set value of objects directly in the old s distribution. This parameter is valid only for the serial collector and ParNew collector, but not for the ParallelGC collector, and defaults to 0.

2.3.1 case

public class GCLog {
    private static final int _1MB = 1024 * 1024;
    public static final int _1KB = 1024;

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

    /** * VM parameters: -xmx7m-xms7m-xmn2m-xx :+PrintGCDetails * large object directly into the old age */
    private static void toOld(a) {
        //byte[] bigB1 = new byte[2 * _1MB];
        //byte[] bigB2 = new byte[3 * _1MB];}}Copy the code

Set VM parameters:

-Xmx7M -Xms7M -Xmn2M -XX:+PrintGCDetails

The object age is not set, so the default is 15. Run three times to close two comments, open the first comment, and open the second comment. The results are as follows:

For the first time,

[GC (Allocation Failure) [PSYoungGen: 1023K->504K(1536K)] 1023K->608K(7680K), 0.0009661 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 1536K, used 1324K [0x00000000ffe00000.0x0000000100000000.0x0000000100000000)
  eden space 1024K, 80% used [0x00000000ffe00000.0x00000000ffecd210.0x00000000fff00000)
  from space 512K, 98% used [0x00000000fff00000.0x00000000fff7e010.0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000.0x00000000fff80000.0x0000000100000000)
 ParOldGen       total 6144K, used 104K [0x00000000ff800000.0x00000000ffe00000.0x00000000ffe00000)
  object space 6144K, 1% used [0x00000000ff800000.0x00000000ff81a040.0x00000000ffe00000)
 Metaspace       used 3431K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 375K.capacity 388K.committed 512K.reserved 1048576K

Copy the code

The second time

[GC (Allocation Failure) [PSYoungGen: 1023K->504K(1536K)] 1023K->640K(7680K), 0.0011018 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 1536K, used 1332K [0x00000000ffe00000.0x0000000100000000.0x0000000100000000)
  eden space 1024K, 80% used [0x00000000ffe00000.0x00000000ffecf2a8.0x00000000fff00000)
  from space 512K, 98% used [0x00000000fff00000.0x00000000fff7e010.0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000.0x00000000fff80000.0x0000000100000000)
 ParOldGen       total 6144K, used 2184K [0x00000000ff800000.0x00000000ffe00000.0x00000000ffe00000)
  object space 6144K, 35% used [0x00000000ff800000.0x00000000ffa22050.0x00000000ffe00000)
 Metaspace       used 3443K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 377K.capacity 388K.committed 512K.reserved 1048576K

Copy the code

The third time

[GC (Allocation Failure) [PSYoungGen: 1023K->504K(1536K)] 1023K->616K(7680K), 0.0009587 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 1536K, used 1375K [0x00000000ffe00000.0x0000000100000000.0x0000000100000000)
  eden space 1024K, 85% used [0x00000000ffe00000.0x00000000ffed9d70.0x00000000fff00000)
  from space 512K, 98% used [0x00000000fff00000.0x00000000fff7e010.0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000.0x00000000fff80000.0x0000000100000000)
 ParOldGen       total 6144K, used 3184K [0x00000000ff800000.0x00000000ffe00000.0x00000000ffe00000)
  object space 6144K, 51% used [0x00000000ff800000.0x00000000ffb1c050.0x00000000ffe00000)
 Metaspace       used 3471K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 381K.capacity 388K.committed 512K.reserved 1048576K

Copy the code

As can be seen from the result, since the 2MB and 3MB objects are larger than the space of the new generation, they are directly allocated to the old generation, causing the memory of the old generation to be occupied. In fact, if the older generation cannot be allocated, then Full GC will be performed and an OOM exception will be raised.

2.4 Distribution on TLAB

If Thread Local Allocation Buffer is enabled, the Allocation Buffer will be allocated to the dedicated TLAB (Thread Local Allocation Buffer) first by Thread. If -xx :+UseTLAB is set, the Allocation Buffer will be allocated to the dedicated TLAB by Thread. Each thread has a separate Buffer. If it wants to allocate memory, it allocates it to its own Buffer. There is no competition, which can improve the efficiency of allocation. Then apply for a piece from Eden area to continue to use) for allocation. The space of TLAB is partitioned from Eden.

TLAB assignment is enabled by default in JDK1.8. Run the Java -xx :+PrintFlagsFinal command to check TLAB assignment.

2.5 Memory space allocation guarantee

Before Minor GC occurs, the virtual machine checks to see if the maximum available contiguous space of the old generation is greater than the total space of all objects of the new generation. If this condition is true, then Minor GC is guaranteed to be safe. If this is not true, the virtual machine checks the HandlePromotionFailure setting to see if the guarantee failure is allowed. If so, it continues to check whether the maximum available contiguous space of the old age is greater than the average size of the objects promoted to the old age, and if so, a Minor GC is attempted, although this Minor GC is risky; If less than, or if the HandlePromotionFailure setting does not allow risk, then do a Full GC instead.

As mentioned earlier, the new generation uses a copy-collection algorithm, but for memory utilization, only one Survivor space is used as a rotation backup, so in the case of a large number of objects surviving the Minor GC, an allocation guarantee from the old generation is required to make the objects that are not Survivor fit directly into the old age. But only if there was enough room for these objects in the old age itself. However, it is impossible to know how many objects survived before the actual memory collection is completed, so we have to take the average capacity of objects in the old age of each previous collection as the empirical value, and compare it with the remaining space of the old age, and decide whether to carry out Full GC to make more space in the old age.

The comparison of averages is still a dynamic probability method, that is, if the number of surviving objects after a Minor GC is much higher than the average, HandlePronotion Failuer will still result. If a guarantee fails, then Full GC will have to be restarted after the failure.

After JDK 6 Update 24, Minor GC is performed whenever the contiguous space of the old generation is greater than the total size of the new generation object or the average size of the successive promotions, otherwise Full GC is performed. The HandlePromotionFailure parameter no longer affects the virtual machine’s space allocation guarantee policy.

2.6 System. The gc ()

System.gc() triggers both the old generation and the new generation to collect. By default, system.gc () takes effect immediately and reclaims the entire heap using the traditional Full GC, Serial+ Serial Old, ignoring the UseG1GC and UseConcMarkSweepGC arguments.

After use – XX: + ExplicitGCInvokesConcurrent System. The gc () this shows the gc would use and have a way of recycling.

In general, we believe that garbage collection happens automatically, without manual triggering. Frequent garbage collection has a great impact on system performance. You can use -xx :+DisableExplicitGC to disable display GC, making System.gc() equivalent to an empty function.

For JVMS with default concurrent collector, such as JDK1.8 using system.gc () triggers a Yong GC before the Full GC. The purpose of this is to collect the new generation first and avoid having to hand over all the collection work to the Full GC at the same time, which can reduce the pause time. -xx: -scavengebeforeFullGC you can turn this feature off. By default, it is true.

In this case, the VM parameter is -xx :+PrintGC

public class GCLog {
    public static void main(String[] args) {
        sysGc();
    }

    private static void sysGc(a) { System.gc(); }}Copy the code

The output shows that JDK1.8 calls the GC method and does gc twice.

[GC (System.gc())  5207K->848K(249344K), 0.0011507 secs]
[Full GC (System.gc())  848K->643K(249344K), 0.0044896 secs]

Copy the code

Related articles:

  1. In-depth Understanding of the Java Virtual Machine

If you need to communicate, or the article is wrong, please leave a message directly. In addition, I hope to like, collect, pay attention to, I will continue to update a variety of Java learning blog!