In-depth analysis of the GC garbage collection mechanism!

Preface

Whenever I see my JVM virtual machine running, I often worry about whether my computer will blow up. It screams, this should take up more memory, and how to clean up the garbage in it. I thought about it. Mr. Wang, who sorts garbage in Shanghai, I showed a kind face. I wanted to go deep into the virtual machine and arrange it for him, so I sorted out this garbage collection mechanism GC with tears.
Insert picture description here

JVM overview

We are all familiar with JVM, which is the abbreviation of Java Virtual Machine (Java Virtual Machine), which is also a fictitious computer, which is realized by simulating various computer functions on the actual computer. JVM has its own complete hardware architecture, such as processor, stack, etc., as well as a corresponding instruction system. It is essentially a program. When it is started on the command line, it starts to execute and save it in a bytecode file. Instructions. The portability of the Java language is based on the JVM. As long as any platform is equipped with a Java virtual machine for the platform, the bytecode file (.class) can be run on the platform. This is "one time Compile, run multiple times".
JAVA GC (Garbage Collection, garbage collection) mechanism is an important feature that distinguishes C++. C++ requires developers to implement the logic of garbage collection by themselves, while JAVA developers only need to focus on business development, because garbage collection is a cumbersome thing JVM Has done it for us. According to the JVM specification, JVM divides the memory into the following areas: 1. Method Area 2. Heap 3. Virtual Machine Stack 4. Native Method Stack 5. .Program counter (Program Counter Register), in which only the method area and the heap area need to be garbage collected, and the objects to be reclaimed are those that do not have any references.

Quotation (four types)

  • Strong reference: Object object = new Object(); This type of reference is the most common in JAVA. As long as the strong reference is still there, it will not be recycled.
  • Soft references: Some objects that may be useful and not necessary will be recycled when the system memory is insufficient.
  • Weak references: Weaker than soft references, objects associated with weak references can only survive until the next GC.
  • False reference: The weakest reference relationship will not affect its survival time at all.

What is rubbish?

When we want to recycle garbage, we must first know what garbage is. We have to talk about martial arts. If it is useful or useless, we will recycle it randomly. In fact, it is to determine which objects are not referenced, occupying the pit and not shit, and then put it on the ground. Confiscated. In the JAVA architecture, almost all object instances are stored in the heap, so garbage collection is implemented for the heap. It is necessary to match the door in the heap.
However, in the eyes of the JVM, the garbage is those in the heap. Objects that exist in the team and are "hanging up". Which ones are "hanging up" must be processed by themselves. I said stop, what algorithm is there?
Insert picture description here

Algorithm for judging garbage

Reference counting

Assuming that each object has a reference counter. When an object is created and initialized assignment, the counter is incremented. Eg: When the object B is assigned to A, then the counter of B is incremented by one. However, when the reference is invalid (some When the reference of an object exceeds the life cycle (after being out of scope) or is set to a new value, the counter of the previously referenced object is reduced by one), and those objects whose counter is zero are called garbage. When an object is treated as garbage When garbage is collected, the value of the counter of any object it references is decremented by one.

  • Advantages: The reference counting method is relatively simple to implement, which is more beneficial to the implementation of an environment where the program is not interrupted for a long time.
  • Disadvantages: additional space is required to store the counter, and it is difficult to detect the previous circular reference of the object.

Accessibility analysis

Reachability means that if an object is directly or indirectly referenced by other reachable objects by at least one variable in the program, the object is said to be reachable.
Reachable conditions:

  • Objects belong to the root set
  • Object is referenced by a reachable object

The objects in the root set include: objects
referenced in the virtual machine stack (local variable table in the stack frame) objects
referenced by constants in the
method area objects referenced by class static properties in the method area objects referenced by the
JNI (Native method) in the native method stack Reference object
Active thread (Java thread that has been started but not stopped)

In the reachability analysis method, an object has two states, either reachable or unreachable. When judging the reachability of an object, the object needs to be marked.

In the root search algorithm, to truly declare an object dead, it must go through at least two marking processes:
if the object finds no reference chain connected to the root object after the root search, it will be marked for the first time and performed once filter. The filtering condition is whether it is necessary for this object to execute the finalize() method (it can be regarded as a destructor, similar to dealloc in OC and deinit in Swift). When the object does not cover the finalize() method, or the finalize() method has been called by the virtual machine, the virtual machine treats both cases as unnecessary.
If the object is determined to be necessary to execute the finalize() method, then the object will be placed in a queue named F-Queue, and later a low-priority Finalizer automatically created by the virtual machine The thread executes the finalize() method. The finalize() method is the last chance for the object to escape the fate of death (because the finalize() method of an object can only be automatically called once by the system at most), and later the GC will perform a second small-scale operation on the objects in the F-Queue Mark, if you want to save yourself successfully in the finalize() method, just let the object re-reference any object in the chain to establish an association in the finalize() method. If the object is not yet associated with any chain references, it will be recycled.

How to recycle garbage?

Mark-sweep algorithm

First mark and then clear, first use the reachability analysis method to mark all the objects to be marked, and then recycle after marking.

  • Advantages: There is no need to move objects, and only non-living objects are processed, which is extremely efficient when there are fewer "hanging" objects.
  • Disadvantages: The marking and removal process is not efficient, and a lot of fragments will be generated after marking.

Mark-up algorithm

The marking process is still the same as above. It is different in the process of handling garbage objects. It does not directly clean up, it tortures, it moves all the objects together, and then directly cleans up the memory. It is more suitable for the old age. Processing.
Advantages: there is no fragmentation problem.
Disadvantages: all objects need to be copied to a new place, and their reference addresses must be updated.

Copy algorithm

First of all, the copy algorithm is more suitable for the new generation. In the old generation, the efficiency of the copy operation will become lower. The mark is still the reachability analysis method.

  • Advantages: The marking phase and the copy phase can be carried out at the same time, just move the top pointer of the stack, and allocate the implementation in order, regardless of fragmentation.

Generational collection algorithm

  • Cenozoic: Almost all newly generated objects are placed in the Cenozoic first.
  • Old age: Large objects will enter the old age directly, and long-lived objects will enter the old age. The default is 15 years old.
  • Permanent generation: The recycling of the permanent generation mainly recycles two parts: discarded constants and useless classes.
  • I will organize this piece into an article in the future.
  • I hope you all give me your advice!

Insert picture description here
Nonsense time:

If the customer thinks that the food is appropriate, can you give a free like! Thank you! Slow walker officer! It is recommended to pack and collect and come again next time. Shop Xiaoer QQ: 309021573, welcome to harass!

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/112802508