Ways to find trash

1. Reference counting: Cannot solve the problem of circular references

CG root includes thread stack variables, static variables, constant pools, JNI Pointers (local method objects used to call C or C++)

Sweep algorithm

1. Mark-sweep is suitable for a large number of survivable objects. Prone to fragmentation. Scan twice

2, copy copy is suitable for the survival of fewer objects. Waste of memory, move copied objects, need to adjust references, scan again

Mark-compact is not prone to fragmentation, does not waste memory, but scans twice to move objects

Generational model

All GC except Epsilon ZGC Shenandoah use logical generation model

G1 is logical generation, not physical generation

In addition not only logical generation, but also physical generation

On the stack

Thread private small object

There is no escape

Thread local Allocation (TLAB)

The local thread size occupies 1% of Eden

Small objects

Multithreading allows you to claim space without competing for Eden

Common garbage collector

1, Serial and serial old

Parallel and parallel old

ParNew and CMS

4, the G1

ZGC and Shenandoah

CMS (Concurrent)

Initial tag (root node only) -> concurrent tag -> relabel (new object produced by concurrent tag) -> concurrent cleanup

The problem of the CMS

Serial Old is used to clean up memory fragments

2. Floating garbage (second recycling)

Concurrent marking algorithm: tricolor marking

Garbage collector versus memory size

  1. Serial dozens
  2. PS hundreds of megabytes – a few gigabytes
  3. CMS – 20G
  4. G1 – hundreds of grams
  5. Zgc-4t-16t (JDK13)

algorithm

Four stages of CMS

1. Initial mark

2. Concurrent markup

3. Re-mark (concurrent mark phase may turn garbage objects into non-garbage objects)

4. Concurrent cleanup

G1 characteristics

  • Concurrent collection
  • Compressing free space does not prolong the GC pause time
  • Easier to predict when GC pauses will take place
  • Suitable for scenarios where high throughput is not required, but the response is fast
  • A CSet (collection Set) stores a collection of partitions that can be reclaimed
  • RememberdSet (RSet) Records the information that two different regions reference each other. Each RSet in a RememberdSet saves the information that is referenced by objects in another region
  • Because of the RSet, the RSet needs to be maintained every time a reference is assigned to an object. The maintenance process is called the GC write barrier, and the GC write barrier is not equal to the memory barrier
  • G1 new and old ages are dynamic, don’t know by hand; this is how G1 predicts pause times

FGC G1 have?

  • G1 has FGC, when object allocation is too fast for the GC thread to recycle
  • G1’s FGC was serial before Java 10 and parallel after that

What if G1 produces FGC?

Expand the memory

Improve CPU performance

Lower the threshold for mixedGC to occur earlier (default 45%)

MixedGC is a complete CMS

Tricolor markers (CMS and G1)

Tricolor is the three states

They are black (complete mark, including only itself and its adjacent nodes, not counting tertiary nodes), gray (complete half mark, only itself marked), and white (unmarked).

About the leakage

In the concurrent marking phase, (if the white marking below the grey is disconnected from the grey and the white is connected to the black). Then there will be a missed mark, because the black has been marked and will not be scanned again

Method to solve missing mark

Because two necessary conditions are missing (if the white mark below the gray is disconnected from the gray, and the white is connected to the black)

So destroy one of them

Incremental Update — Batch update, rescan next time (CMS solution)

2. If gray and white references are kept in the GC stack, white can still be scanned by the GC. In fact, there is no reference in the gray program, but the GC made a reference, and then when rescan, scan the reference set. SATB (Snapshot at the beginning) G1 solution

Why use SATB instead of Incremental Update for G1?

There are fewer scans, and if the black turns gray, the gray needs to be scanned again

The next time you scan, because of the RSet, you don’t need to scan the whole heap for white references

Color pointer (ZGC)