[JVM basics]-garbage collection basics (GC related)

1. Definition of garbage

1. What is rubbish

When an object in memory cannot find any reference, this object is a garbage object.

2. What is a memory leak

Memory leak (memory leak) means that the dynamically allocated heap memory in the program is not released or cannot be released for some reason, which causes a waste of memory, slows down the running speed of the program and even crashes the program.

3. What is STW

STW stands for stop the world, which refers to the suspension of all business threads when the JVM performs GC.

2. Garbage search algorithm

1. Reference counting algorithm (reference count)

(1) Definition

Add a reference counter to each object. Whenever there is a new reference, the counter is +1, and the counter is -1 after the end of the reference. An object whose counter is 0 at any time is not referenced.

(2) Features

Advantages:
1. The reference counting algorithm has real-time properties when recycling garbage. When an object's reference is 0, it will be recycled directly, and the memory can be released without waiting for a specific time.
Disadvantages:
1. When there are circular references between objects, the garbage collection period cannot determine whether these objects are garbage, so the circularly referenced objects cannot be recycled. (Memory leak)

2. Root searching algorithm

(1) Definition

Search downwards from the GC Roots node (starting node). If there is no reference chain (that is, the GC root is unreachable), it proves that the object is unavailable
Insert picture description here

(2) Which objects can be used as GC Root

  1. Objects referenced in local variables (also called local variable tables) in the virtual machine stack
  2. The method goes to the static variable of the class, the object that the constant references
  3. Objects referenced by JNI (native method) in the native method stack

(3) Features

The essence of tracing GC is to identify the remaining space as "useless" by finding all live objects, rather than finding all dead objects and reclaiming the space they occupy. GC roots is the starting point of tracing GC. To achieve semantically correct tracing GC, all GC roots must be fully enumerated. Otherwise, objects that should be alive may be missed, causing the GC to incorrectly reclaim these missing live objects.

3. Garbage Collection Algorithm (GC Algorithms)

1. Mark-sweep algorithm (mark-sweep)

(1) Definition

Mark out the objects that need to be cleared and remove them.

(2) Realization

The implementation of the mark removal algorithm is divided into two stages:

  1. Marking stage: Marking out surviving objects
  2. Recycling stage: Reclaim the space occupied by unmarked objects The
    specific process is as follows:
    Insert picture description here

(3) Features

Advantage:
Only the surviving objects are marked. After marking, unmarked objects in the entire space are scanned for recycling. The algorithm does not need to move objects, only needs to process non-surviving objects, which is highly efficient.
Disadvantages
Because the non-survivable objects are directly recycled, and the memory is not organized, memory fragmentation will occur. When there are many memory fragments, when a large object enters the memory space and cannot allocate enough memory for the period, GC will be triggered in advance.

2. Copy algorithm (copy)

(1) Definition:

The memory is divided into two, using one area at a time. When gc is triggered, the surviving object is copied to another area and the original area is cleared.

(2) Realization

At the beginning, it divides the heap into an object surface and multiple free surfaces. The program allocates space for the object from the object surface. When the object is full, the garbage collection based on the copying algorithm scans the active objects from the root set (GC Roots), and Each active object is copied to the free surface (so that there is no free hole between the memory occupied by the active object), so that the free surface becomes the object surface, the original object surface becomes the free surface, and the program will be in the new object surface Allocate memory.
Insert picture description here

(3) Features

Advantages :

  1. Only scan once, high efficiency.
  2. The implementation method is to copy and move the object without memory fragmentation

Disadvantages:

  1. Because the memory is divided into two areas, one area remains free, so the memory waste is large
  2. Object references will be adjusted when reconsidering moving objects

3. Mark-compack

(1) Definition

Optimized implementation of mark removal algorithm, which compresses space while removing garbage objects.

(2) Realization

The marking phase of this algorithm is the same as Mark-Sweep, but after marking is completed, it does not directly clean up recyclable objects, but moves all surviving objects to one end, and then cleans up useless objects.
Insert picture description here

(3) Features

Advantages:
1. Because the space is organized, memory fragmentation will not occur.
Disadvantages
1. Because it has been scanned twice and sorting is added on the basis of cleaning, the time cost is high.

Interview questions

1. Is the root reachability algorithm a depth-first algorithm or a breadth-first algorithm?

The young generation prefers breadth first, and the old generation prefers depth first.

Guess you like

Origin blog.csdn.net/xiaoai1994/article/details/109640045