JVM----GC (garbage collection) detailed explanation

1. Introduction to Automatic Garbage Collection

Automatic Garbage Collection (automatic garbage collection) is a feature of the JVM. The JVM will start the relevant thread, which will check the heap memeory in rotation and determine which ones are unreferenced (unreferenced), that is, not used; which ones are Referenced, that is, what is being used.
In the C/C++ language, object memory is allocated and recycled manually, while in the JVM, the JVM uses a garbage collector (a thread) to automatically allocate and recycle memory.

2. Introduction to the basic mechanism of GC

Among the GC mechanisms, the most common and basic solution isMarking–Deletion, examples are as follows:

Step 1 : Marking

The first step in the garbage collection mechanism is usually marking, which is used to confirm which objects are referenced and which ones are unreferenced.
Insert image description here
Referenced objects are marked blue and unreferenced objects are marked yellow. All objects are scanned for marking, which can sometimes be very time-consuming.

Step 2 :Normal Deletion

This node will delete unmarked objects to free up memory space.
Insert image description here

Step 2a : Deletion with Compacting

To further improve performance, in addition to deleting unreferenced objects, you can also compact remaining referenced objects. This makes new memory allocations easier and faster by moving referenced objects together.
Insert image description here

3. JVM Generations

In order to improve the performance of the JVM, the heap memory is broken down into smaller parts or generations, namely Young Generation, Old or Tenured Generation, and Permanent Generation. An example diagram is as follows:
Insert image description here
Young Generation is the memory area where new objects are allocated and marked with an age value. When the Young Generation memory space is full, a minor garbage collection will be triggered (the triggering conditions vary according to the selected garbage collector). Minor garbage collection can be optimized through relevant setting values; if the Young Generation is full of unused objects (dead objects), minor garbage collection will be very efficient and garbage collection will be very fast. Some surviving objects will be marked with age values ​​and eventually transferred to the Old or Tenured Generation.
Stop the World Event Every minor garbage collection will trigger the "Stop the World Event" event, which will cause all threads in the application to pause until the garbage collection operation is completed.
Old Generation will store long-term surviving objects. Generally, a critical point will be set for the age value of the object. When the age value of the object reaches the critical point, it will be moved to Old Generation. Finally, when the Old Generation memory space is full, A major garbage collection will be triggered.
Major garbage collection will also trigger the "Stop the World Event" event. Major garbage collection will be very slow because it involves all objects.
Permanent Generation will store some metadata required for JVM operation. These metadata describe classes, methods, etc.

Guess you like

Origin blog.csdn.net/qq_41768644/article/details/132699708