Advanced JVM 2 - GC Garbage Collection

JVM Basics 1 - Class Loading
JVM Basics 2 - Instruction Set
JVM Advanced 1 - Memory Model
JVM Advanced 2 - GC Garbage Collection
JVM Overview - JVM Architecture

1. Introducing the question: What is garbage?

Garbage : no references point to it

C++ vs Java

java

  1. GC processing garbage
  2. High development efficiency, low execution efficiency

C++

  1. Dispose of trash by hand
  2. forgetting to recycle (may cause memory leaks)
  3. Recycled multiple times (resulting in illegal access)
  4. Low development efficiency, high execution efficiency

2. How do I find trash?

2.1 Reference counter

Clear the counter with 0, not used, inefficient

Disadvantages : A counter needs to be allocated for each object, and the counter itself will be consumed, and it cannot solve the problem of circular references.

insert image description here

2.2 Root reachable algorithm:

Solve the problem that circular references cannot be used in reference counters

When the program is running, the root object is taken out, and the search starts from the root object. The objects that cannot be found in the end are regarded as unfindable by the root object, that is to say, the objects that cannot be found are regarded as garbage.

root object :

  1. JVM stack (thread stack variable)
  2. native method stack (native method stack)
  3. static references in method are a (static references in the method area)
  4. run-time constant pool (running objects in the constant pool)

insert image description here

As shown in the figure: The objects that refer to each other circled in red will be regarded as garbage, because they cannot be found from the root object.

3. Find garbage, how to remove them?

3.1 Mark-Sweep (Mark Sweep)

Two-pass scan, first scan the mark, then clear

insert image description here

What can be found from GC Root is not recyclable

  • Advantages : The algorithm is relatively simple, suitable for high efficiency when there are many objects ( not suitable for Eden Park )
  • Disadvantages : scanning on both sides, low efficiency, prone to debris

3.2 Copying

insert image description here

Divide the memory into two, copy the upper ones that can be found from the GC root to the lower ones, clear the upper ones, and put the surviving ones below

  • Advantages : only scan once, no fragmentation, high efficiency, suitable for situations with few surviving objects (suitable for Eden Park)
  • Disadvantages : Waste half of the space, move the top to the bottom, move and copy the object, need to adjust the reference of the object

3.3 Mark-Compact (mark compression)

Scan twice, first find an empty location, and then move the surviving object to that location

insert image description here

  • Advantages : No memory fragmentation, convenient object allocation, no memory halving
  • Disadvantage : scan twice, need to move the object, and the efficiency is low (if multi-threading handles this problem, it also needs to involve the synchronization problem between multiple threads)

Summarize:

Memory efficiency : copy algorithm > mark removal algorithm > mark compression algorithm (time complexity problem)

Memory uniformity : copy algorithm = mark compression algorithm > mark removal algorithm

Utilization : Mark Compression Algorithm = Mark Sweep Algorithm > Copy Algorithm

Thinking : Is there no better algorithm? Answer: No, there is no best algorithm, only the most suitable algorithm

Young Generation Features:

  • low survival rate
  • Copy the algorithm!

Old Generation Features:

  • High survival rate, large area
  • Mark sweeping (when memory fragmentation is not too much) + mark compression hybrid implementation

4. Heap memory logical partition:

insert image description here

After the class loader reads the class file, what will it usually put in the heap?
Classes, methods, constants, variables ~, save the real objects of all our reference types;
the heap memory is also subdivided into three areas:
Newborn area (eden (Eden Park) + survivor (survival area) ) Young/New
● Old retirement area old
Permanent area Perm ( has been removed in JDK1.8 and replaced by Metaspace (metaspace) in the method area )

4.1survivor (survival area):

insert image description here

The object originally in the Eden area was moved to the survivor area without being cleared after a GC. In the two survivor areas (from area, to area), it has not been eliminated after multiple copying algorithms (after reaching the age, generally 15 light GC (corresponding to the 4 bits of the GC age flag)), it will be transferred to the Old area

  • If the objects in the from area -> old area exceed 50%, the older objects in the from area will be placed in the old area
4.2 GC concept:

insert image description here

  • MinorGC/YGC : trigger when the young generation space is exhausted
  • MajorGC/FullGC : The old area cannot continue to allocate space, and the new area and the old area are GCed together
4.3 Object allocation process:

insert image description here

Allocation on the stack, thread-local allocation of TLAB

insert image description here

5. Garbage remover:

insert image description here

5.1 Serial collector

Use memory size:

The Serial (serial) collector is the most basic and has the longest history of development. It is a new generation collector that uses a copy algorithm . It used to be (before JDK 1.3.1) the only choice for the new generation of virtual machine collection. It is a single-threaded collector , but "single-threaded" does not mean that the collector will only use one CPU or one collection thread to complete garbage collection work, and more importantly, it must suspend all other work during garbage collection thread until the end of the Serial collector collection ("Stop The World") . This work is automatically initiated and completed by the virtual machine in the background. It is unacceptable for many applications to stop all the normal working threads of the user without the user's visibility.

insert image description here

The collector is a good choice for virtual machines running in Client mode

Applicable to: less than tens of MB of memory

5.2 Parallel Scavenge Collector

Default GC po (Parallel old) + ps (Parallel Scavenge)

The Parallel Scavenge collector is also a parallel multi-threaded new generation collector that also uses the copy algorithm . The characteristic of the Parallel Scavenge collector is that its focus is different from other collectors. The focus of collectors such as CMS is to minimize the pause time of user threads during garbage collection, while the goal of the Parallel Scavenge collector is to achieve a controllable Throughput.

insert image description here

Multi-threaded garbage removal

Applicable to: hundreds of megabits - several G

5.3 ParNew Collector

The ParNew collector is a multi-threaded version of the Serial collector , and it is also a new generation collector. In addition to using multithreading for garbage collection, the remaining behaviors include all control parameters available to the Serial collector, collection algorithm (copy algorithm), Stop The World , object allocation rules, recycling strategies, etc. are exactly the same as the Serial collector, and both share the same Quite a lot of code.

In addition to the Serial collector, currently only it can work with the CMS collector (Concurrent Mark Sweep) . The CMS collector is an epoch-making collector launched by JDK 1.5.

insert image description here

The difference from Parallel Scavenge : Some changes have been made to work with CMS , and PerNew will start when CMS is suitable for a certain stage

5.4 CMS Collector

Algorithm: three-color marking + Incremental Update

The CMS (Concurrent Mark Sweep) collector is a collector that aims to obtain the shortest recovery pause time . It is very suitable for those Java applications that are concentrated on the server side of Internet sites or B/S systems. These applications attach great importance to services. response speed. It is implemented based on the "mark-sweep" algorithm.

The entire process of CMS collector work is divided into the following 4 steps:

  1. Initial mark (CMS initial mark) : Just mark the objects that GC Roots can directly relate to , the speed is very fast, and you need to "Stop The World".

  2. Concurrent mark (CMS concurrent mark) : The process of GC Roots Tracing takes the longest time in the whole process.

  3. Re-marking (CMS remark) : In order to correct the mark record of the part of the object whose mark changes due to the continued operation of the user program during the concurrent mark period , the pause time of this stage is generally slightly longer than the initial mark stage, but far longer than the concurrent mark The time is short . "Stop The World" is also required at this stage.

  4. Concurrent sweep (CMS concurrent sweep) , there will be floating garbage (garbage generated during concurrent sweep)

insert image description here

Since the longest concurrent marking and concurrent clearing process collector threads in the entire process can work together with user threads (try not to have FGC, but there will still be).

There is a problem:

  1. Memory Fragmentation memory fragmentation
  2. Floating Garbage floating garbage (solution: lower the threshold of FGC, reserve a certain space for floating garbage)

Applicable to: about 20G

5.5 G1 (garbage first) collector

Algorithm: Three-color marking + SATB

The G1 (Garbage-First) collector is one of the most cutting-edge achievements in the development of collector technology today. It is a garbage collector for server-side applications . The mission entrusted by the HotSpot development team is to (in a long-term) future Replaces the CMS collector released in JDK 1.5.

Conceptual partition, does not exist physically

insert image description here

features

  • Concurrent collection : Parallel and concurrent G1 can make full use of the hardware advantages in multi-CPU and multi-core environments, and use multiple CPUs to shorten the "Stop The World" pause time. Some other collectors originally need to stop the GC actions executed by Java threads. G1 collects The server can still allow the Java program to continue executing in a concurrent manner.
  • Generational collection Like other collectors, the generational concept is still preserved in G1. Although G1 can independently manage the entire GC heap without the cooperation of other collectors, it can use different methods to deal with newly created objects and old objects that have survived for a period of time and survived multiple GCs to obtain better collection results .
  • Space integration G1 is a collector implemented based on the "mark-sort" algorithm as a whole, and it is implemented based on the "copy" algorithm from a local (between two Regions) perspective. This means that G1 will not generate memory space fragmentation during operation, and can provide regular available memory after collection. This feature is beneficial to the long-running of the program . When allocating large objects, the next GC will not be triggered in advance because no continuous memory space can be found.
  • It is easy to predict the pause time of GC : Predictable pause is a major advantage of G1 over CMS. Reducing pause time is a common concern of G1 and CMS, but G1 can also establish a predictable pause time model in addition to reducing pauses. It is almost a feature of the real-time Java (RTSJ) garbage collector to allow users to specify that within a time segment of M milliseconds, the time spent on GC should not exceed N milliseconds.

Applicable to: hundreds of G memory

card table:

insert image description here

The card class is for pagination, and the card records each card

  • When doing YGC, the entire Old area needs to be scanned, which is very inefficient, so the JVM designed CardTable. If there is an object in the CardTable in the Old area pointing to the Y area, it will be set as Dirty. In the next scan, only the Dirty Card needs to be scanned. Table, in terms of structure, Card Table is implemented using bitMap
Cset (Collection Set)

A table, which contains cards that need to be recycled

Rset (RememberedSet)

insert image description here

There is no need to scan the entire table, and it records who referenced my object. When checking the reference, you don’t need to check other people’s entire table, you only need to scan your own Rset, which is also the key to efficient recycling of G1

What to do if G1 generates FGC?
  1. expand memory
  2. Improved CPU performance (faster recycling)
  3. Lower the trigger threshold of MixedGC (similar to the CMS process), so that MixedGC occurs earlier and garbage collection occurs earlier (default is 45%)

insert image description here

G1 normal garbage collection: MixedGC, FullGC uses serial

6. Three-color marking method:

  • White : not checked
  • Gray : It has been checked, but its members have not been checked (it can be considered that it has been visited, but it is being checked, that is, those nodes in the queue in the traversal of the graph)
  • Black : Both self and members have been checked
Lost Object Problem (漏标)

When we mark, the user Application is still running, he may modify the structure of this graph, and introduce a new problem of missing mark ( originally a living object, but because it has not been traversed, it is regarded as garbage collection ):

insert image description here

Solution:

  1. Incremental update (incremental update) , pay attention to the increase of references, remark black as gray, and rescan attributes next time ( used by CMS )
  2. SATB (sanpshot at the beginning) focuses on the deletion of references. When most B->D disappears, this reference should be pushed to the GC stack to ensure that D can still be scanned by GC ( G1 use )
Why does G1 use SATB? (interview questions)

When the gray->white reference disappears, if there is no black reference to white, the white reference will be pushed to the stack, and this reference will be obtained in the next scan. Due to the existence of Rset (there is no need to scan the entire region, saving time), there is no need to scan the entire heap area to find references pointing to white, the efficiency is relatively high, and the efficiency of SATB with Rset is high

Guess you like

Origin blog.csdn.net/The_xiaoke/article/details/124316761