This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

A. GC algorithm

1. Object survival judgment algorithm

Reference counting algorithm and accessibility analysis algorithm

2. Check whether the object is alive

First mark: the unreachable object in the reachability analysis algorithm will be marked and filtered. The filtering condition is that it is necessary to finalize() method for this object. When finalize() has not been rewritten or the method has been executed, it is not necessary to finalize()

Second flag: If finalize is necessary, the object will be put in the F-queue and executed by the Finalizer thread. The GC will mark the queue a second time simply by re-associating itself with any object on the reference chain, such as assigning itself (this) to a class variable or establishing a reference to a member variable.

③ It should be noted that Finalize () of any object can only be called by the system once

Garbage collection algorithms

Mark clearing algorithm

Mark all objects that need to be reclaimed and recycle them when they are done. It is inefficient and generates a large number of discrete memory fragments

Replication algorithm

The memory is split in two, using only one block. When this block is used, live objects are copied to the other block and cleaned up

Mark-collation algorithm

After marking, the surviving objects are moved to one end, clearing part of the memory outside the end boundary

Generational collection algorithm

Divide the Cenozoic and the old. The new generation will die a lot of objects every time, so use the copy algorithm; Old age objects have a high survival rate, there is no extra space as a guarantee of allocation, use the marking algorithm

Ii.GC type and parameters

GC type

There are three main TYPES of GC for the JVM, the generation GC, the old-generation GC, and the heap GC.

  1. Minor GC: Young GC, which is triggered when space cannot be allocated for a new object, so this GC is faster to collect
  2. Major GC: Old GC, usually accompanied by at least one Minor GC, is typically 10 times slower than Minor GC
  3. Full GC: heap space GC. When the old generation is Full, the Full GC will collect the young generation and the old generation is permanently Full, which will cause the unloading of the Class and Method metadata

When system.gc() is called, or the old heap is Full,

Cenozoic GC processes

During the program’s run, Minor GC was the most frequent, as you can see from the jstat command. The process of Young GC is as follows;

  1. For the first Minor GC when Eden is full, the live object is moved to S0 and Eden is emptied
  2. When Eden is full, GC is triggered. Objects in Eden and S0 are copied into S1, and S0 and Eden are emptied
  3. When GC is triggered, S1 and S0 switch roles, and an object is copied 16 times and sent to the old age
  4. The two survivor avoid fragmentation of memory, reducing old GC and triggering Full GC

Gc startup parameters

At startup time, we need to add the JVM startup parameters so that we can print out the GC information we want to see, which is often used for JVM troubleshooting and optimization.

  1. -verbose:gc: displays the garbage collection process
  2. -verbose: gc-xloggc :$CATALINA_HOME/logs/gc.log: specifies the directory and file name of the GC
  3. -xx :+PrintGCTimeStamps -xx :+PrintGCDetails: Sets the GC log format
  4. -xx :+PrintCommandLineFlags: Displays the default VM parameters

Ratio of new generation to old generation

In general, the ratio of new generation, old generation, and survivor region does not need to be set and the default values are used.

Default values are as follows:

  1. — XX:NewRatio=2: The ratio of Cenozoic to old age is 1:2
  2. – XX:SurvivorRatio=8: means Eden: S1: S2 = 8:1:1