Excellent: During the running process, you can check the object counter at any time, perform GC, and the GC process, the application does not need to be paused, the execution speed is fast (a single object GC will not affect other objects), insufficient memory, OOM
Lack: There is a circular reference problem (A refers to B, B refers to A, A=null, B=null. A, B will never GC), always in GC, occupying CPU

Excellent; solve the circular reference problem
Disadvantages: low efficiency, need to traverse all objects, serious fragmentation, cleared memory is not continuous, when new large objects are easy to explode OOM

Excellent: Solve the fragmentation problem of mark removal
Lack: Need to move the memory location, lower efficiency

Excellent: Solve the memory movement problem of mark clearing
Lack: The waste of space is more serious, not suitable for the case of less garbage in memory space

1. Before GC starts, the objects are distributed in Eden, s0 area, s1 area is empty
2. GC starts, all the surviving objects in Eden are copied to s1, and the surviving objects in the s0 area decide their destination according to their age value
Use -XX:MaxTenuringThreshold to set the age threshold, if the threshold is exceeded, the object in s0 is moved to the old age, and the object in s0 is not reached, the object is moved to s1
3. GC is complete, clear Eden, s0 area, s0 and s1 exchange roles, repeat step 1 until "s1" is filled, and then move all objects in "s1" to the old generation
Excellent: When there are many garbage objects, high efficiency and no fragmentation
Deficiency: Less garbage, not applicable, such as: old age, s0/s1 can only use one of them at a time, low memory usage
/**
* Test GC collector
* @author regotto
*/
public class GcTest {
public static void main(String[] args) {
ArrayList<Object> objects = new ArrayList<>();
while (true) {
if (System.currentTimeMillis() % 2 == 0) {
//Generate a lot of discarded objects
objects.clear();
} else {
for (int i = 0; i < 10000; i++) {
Properties properties = new Properties();
properties.put("key:" + i, "value:" + System.currentTimeMillis());
objects.add(properties);
}
}
try {
Thread.sleep(new Random().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Set up VM Optional: Use serial GC device, print GC details

The running result is as follows:

Log interpretation (using the first row of data):
GC: Young Generation GC; Full GC: All GC in all spaces
DefNew: Use serial GC device
4416k -> 512k(4928k): Before GC, young generation objects occupy 4416k space, and GC young generation objects occupy 512k space, for a total of 4928k space
0.0019950secs: GC time spent
7318k -> 3975k (15872k): heap space GC situation

-XX:+UseParallelGC The young generation uses ParallelGC garbage collector, and the old generation uses serial collector.
-XX:+UseParallelOldGC The young generation uses the ParallelGC garbage collector, and the old generation uses the ParallelOldGC garbage collector.
-XX:MaxGCPauseMillis sets the maximum pause time during garbage collection, in milliseconds
It should be noted that in order to reach the set pause time, ParallelGC may adjust the heap size or other parameters.
If the heap size is set small, it will cause GC work to become very frequent, but may affect performance. Use this parameter with caution.
-XX:GCTimeRatio Set the percentage of garbage collection time to program running time, the formula is 1/(1+n). Its value is a number between 0 and 100, and the default value is 99, which means that the garbage collection time cannot exceed 1%
-XX:UseAdaptiveSizePolicy Adaptive GC mode, the garbage collector will automatically adjust the young generation, old generation and other parameters to achieve a balance between throughput, heap size, and pause time. It is generally used in scenarios where it is difficult to manually adjust parameters and let the collector adjust automatically.

The test code is the same as the previous one, only the VM options are modified, and the running results are as follows:


InitialMarking: Mark root, Stop-the-world appears
Marking: Marking objects, running simultaneously with the application thread
preclean: pre-clean, run simultaneously with the application thread
finalMarking: Mark again, because it runs at the same time as the application thread, the previous marking does not solve the problem, and this process appears Stop-the-world
sweeping: concurrent sweeping, running simultaneously with the application thread
resizing: Adjust heap size, clean up debris, compress space
resetting: reset, wait for the next CMS to be triggered, run simultaneously with the user thread

The log printed by the program also follows the above process, and the program running results are as follows:

G1 cancels the traditional new generation, physical division of the old generation, and changes the memory space into several regions, each of which contains logically the new generation and the old generation. G1 has YoungGC, MixedGC, and FullGC, which are triggered under different conditions
Excellent: Each area has multiple states (Old, Eden, Humongous, Survivor) to solve the fragmentation problem, even in the normal processing process, can solve the memory compression problem

Eden area space exhaustion trigger, EdenGC, data in Eden is moved to Survivor area (Survivor is full, data is moved to new Survivor area, part of data is moved to Old area), part of data is moved to Old area, current Eden is cleared, and becomes Unused area

RememberSet solves the problem of finding root objects in the new generation. Each area initialization generates a RememberSet. This set saves records of other objects referencing "I". Scanning RememberSet can get the reference relationship between objects, and no longer need to refer to the new generation. In the old age, all objects in the old age are scanned.

In order to avoid exhaustion of heap memory, JVM starts MixedGC to reclaim all Young area and part of Old area (MixedGC is not FullGC).
Use -XX:InitiatingHeapOccupancyPercent=n (the threshold of the percentage of the old age to the total heap size) to determine when MixedGC is triggered
MixedGC is divided into 2 steps: global concurrency mark (first 5 steps), copy live objects (6th step)

-XX:+UseG1GC Use G1 garbage collector
-XX:MaxGCPauseMillis Set the expected maximum GC pause time index (JVM will try its best to achieve it, but it is not guaranteed to be reached), the default value is 200 milliseconds.
-XX:G1HeapRegionSize=n sets the size of G1 region. The value is a power of two and the range is between 1 MB and 32 MB. The goal is to divide approximately 2048 regions based on the smallest Java heap size. The default is 1/2000 of the heap memory.
-XX:ParallelGCThreads=n Set the value of the number of STW worker threads. Set the value of n to the number of logical processors. The value of n is the same as the number of logical processors, up to eight.
-XX:ConcGCThreads=n sets the number of threads marked in parallel. Set n to about 1/4 of the number of parallel garbage collection threads (ParallelGCThreads).
-XX:InitiatingHeapOccupancyPercent=n Sets the Java heap occupancy threshold that triggers the marking cycle. The default occupancy rate is 45% of the entire Java heap.
‐XX:+PrintGC output GC log
‐XX:+PrintGCDetails output detailed GC log
‐XX:+PrintGCTimeStamps output GC timestamp (in the form of base time)
‐XX:+PrintGCDateStamps output GC timestamp (in the form of date, such as 2013-05-04T21:53:59.234+0800)
‐XX:+PrintHeapAtGC prints out heap information before and after GC
-Xloggc:../logs/gc.log output path of the log file

After the code runs, the GC log will be automatically entered into gc.log under the project
Use GC Easy for analysis (http://gceasy.io)
Upload gc.log to this website, you can perform GC analysis and obtain analysis report
First, the type of garbage collector The garbage collector needs to involve a concept STW, that Stop The World, when carried out gc, you may need to stop thread ongoing task. There are the following g...
GC (Garbage Collection) that is garbage collection, GC is the JVM an automatic memory management. Whether automatic memory management system can automatically determine the specified memory area to be...
A new generation 1. Serial (single-threaded, replication algorithm) Only use a CPU or a thread to complete garbage collection, in refuse collection and at the same time, we must suspend all other work...
05 GC garbage collector 5.1 Serial garbage collector (single-threaded, replication algorithm) 5.2 ParNew garbage collector (Serial + multithreading) 5.3 Parallel Scavenge collector (multi-threaded rep...
GC principle-garbage collector Garbage collector If the collection algorithm is a method of memory recycling, the garbage collector is the specific implementation of memory recycling Serial collector ...
1. Memory recovery Reference counting method: Add a reference counter to the object. Whenever there is a reference to it, the counter is incremented by 1; when the reference is invalid, the counter is...
0. Preface The garbage collector mainly reclaims objects in the JVM heap. The JVM heap is mainly divided into two parts: young generation and old generation. There is also a permanent generation in th...
G1 garbage collector of GC table of Contents Features of previous collectors What is G1 G1 features G1 underlying principle G1 recycling steps Advantages compared to CMS Small summary 1. Features of p...
1. Serial young generation "Single-threaded" collector means that when garbage collection is performed, all other worker threads must be suspended until the end of ...