This is the second day of my participation in the August More text Challenge. For details, see: August More Text Challenge

GC

Introduction (master skills)

Familiar with common GC algorithms, familiar with common garbage collectors, with actual JVM tuning experience

Basics of GC

What is garbage

One or more objects without any reference (circular reference)

Garbage collection (Java vs c++)

  1. java
  • Garbage disposal by GC
  • High development efficiency, low execution efficiency
  1. C++
  • Manual disposal of garbage
  • Low development efficiency, high execution efficiency

Automatic memory recovery, programming is simple, the system is not prone to error, manual release of memory, prone to two types of problems:

  1. Forget to reclaim (memory leak)
  2. Multiple reclaim (illegal access)

How to Locate garbage

  1. Reference counting (ReferenceCount)

Can’t solve the circular reference problem

Common garbage collection algorithms

  1. Mark sweep
  • Position discontinuous, easy to produce debris, low efficiency
  • Double scan: The first scan is for unrecyclable objects, and the second scan is for recyclable objects
  • [Efficiency is high when there are more surviving objects]
  1. Copying algorithm
  • No fragmentation, wasted space, moving copies of objects, need to adjust references to objects
  • [Applicable to the case where there are few surviving objects]
  • Scan only once, improve efficiency, suitable for Eden zone

  1. Mark Compact
  • No fragmentation, no memory halving, easy object allocation
  • The object needs to be moved and the pointer needs to be adjusted, so the efficiency is low

JVM memory generational model (for generational garbage collection algorithms)

An object goes from birth to death

The concept of GC

  1. Part of the model used by garbage collectors

    All GCS except Epsilon ZGC Shenandoah use the logical generational model

    G1 is a logical generation, not a physical generation

    In addition not only logical generation, but also physical generation

  2. Cenozoic + Old + permanent (1.7) Perm Generation/ Metaspace (1.8

    1. Permanent generation metadata – Class
    2. Permanent generation must specify size limit, metadata can be set or not set, no upper limit (limited to physical memory)
    3. String constants 1.7 – permanent generation, 1.8 – heap
    4. MethodArea Logical concept – permanent generation, metadata
  3. Cenozoic = Eden + 2 Suvivor

    1. After YGC collection, most of the objects will be collected and enter S0 alive
    2. YGC again, the living object Eden + s0 -> s1
    3. Again YGC, Eden + s1 -> s0
    4. Adequate age -> old age (15 CMS 6)
    5. Block S won’t fit -> Old age
  4. The old s

    1. diehard
    2. The old age is Full FGC Full GC
  5. GC Tuning (Generation)

    1. Minimize FGC
    2. MinorGC = YGC
    3. MajorGC = FGC
  6. Object allocation process diagram

  7. Dynamic age: (not important) www.jianshu.com/p/989d3b06a…

  8. Distribution during the period of guarantee: (not important) YGC survivor area space is not enough space guarantee directly into old age Reference: cloud.tencent.com/developer/a…

Object allocation

  • On the stack

    • Thread private small objects
    • There is no escape
    • Support for scalar substitution
    • Don’t need to adjust
  • TLAB (Thread Local Allocation Buffer)

    • Eden (1% by default) (Each thread has a private space in Eden, which is allocated when the thread is created)
    • When multithreading, you can apply for space without competing for Eden, improving efficiency
    • Small objects
    • Don’t need to adjust
  • The old s

    • The big object
  • eden

    • New object

Mock instance code

package com.mashibing.jvm.c5_gc;

//-XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:-UseTLAB -Xlog:c5_gc*
Escape analysis scalars replace thread-specific object allocations

public class TestTLAB {
    class User {
        int id;
        String name;

        public User(int id, String name) {
            this.id = id;
            this.name = name; }}void alloc(int i) {
        // The object has no reference elsewhere, so no escape occurs
        new User(i, "name " + i);
    }

    public static void main(String[] args) {
        TestTLAB t = new TestTLAB();
        long start = System.currentTimeMillis();
        for(int i=0; i<1000 _0000; i++) t.alloc(i);
        longend = System.currentTimeMillis(); System.out.println(end - start); }}Copy the code

When the object enters the old age