Home  >  Article  >  Web Front-end  >  A brief analysis of the heap and garbage collection mechanism

A brief analysis of the heap and garbage collection mechanism

hzc
hzcforward
2020-07-03 09:20:081840browse

In this article we mainly focus on these issues:: After the Java program is executed, when will the objects in the heap be recycled? How to recycle?

The heap is also called the "GC heap." Since collectors now basically use generational collection algorithms, the Java heap can also be subdivided into: new generation and old generation. The ratio is 1:2; to be more detailed, the new generation is divided into Eden area and Survivor area, and the ratio is 8:1. The following figure shows the structure of the heap:

##The allocation of memory for objects in the heap is strictly regulated , the strategy is:

  • Objects allocate memory in the new generation Eden area first;

  • ## Large objects enter the old generation directly, mainly long strings and arrays, which require a large amount of continuous memory space;

  • Long-term surviving objects enter the old generation. When the memory in the Eden area is insufficient, the JVM initiates a MinorGC, and the age of the object is increased by one. The default object age reaches 15 and enters the old age;

  • Dynamic age determination. The sum of the sizes of all objects of the same age is greater than half of the Survivor space. Objects greater than or equal to this age enter the old generation

The new generation GC refers to the Minor GC. Garbage collection in the new generation is frequent and fast. Old generation GC (Major GC/Full GC) performs garbage collection in the old generation, usually accompanied by at least one minor gc. Slow. Full GC will be triggered in the following situations:

    Insufficient space in the old generation;
  1. Insufficient space in the method area;
  2. Call System.gc(), it is recommended that the JVM perform full gc;

  3. Long-term surviving objects are transferred to the old generation , insufficient space;

  4. There is not enough contiguous space allocated to large objects;

  5. There are too many objects that survive garbage collection in the new generation, and S1 cannot fit them in. The guaranteed space in the old generation is insufficient. The guaranteed space refers to whether the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation.

Almost all objects are placed in the heap, so how do we know whether these objects are still useful? JVM provides two methods to determine:

  • ##Reference counting method: The object adds a reference counter. Every time it is referenced, the counter value increases by one. When the reference becomes invalid, the counter value decreases by one. When the number of references is 0, it means that the object is not alive. The reference counting method cannot solve the circular reference problem. There are detailed examples in Teacher Zhou Zhipeng's book, which is relatively easy to understand.

  • #reachability analysis method : Taking the "GC Roots" object as the starting point, just like the root node of a tree, search downwards. The path traveled by the search is called a reference chain. If there is no reference chain from an object to the starting point of GC Roots, then this The object is unreachable and needs to be recycled. GC Roots refers to objects referenced by the virtual machine stack, objects referenced by the local method stack, objects referenced by static properties in the method area, and objects referenced by constants in the method area.

## References were mentioned above. The survival of objects is related to references. Reference types are divided into strong references, soft references, weak references, and virtual references.

Strong reference, new object, the garbage collector will never recycle it;

  • Soft reference, the memory of these objects will be recycled before OMM occurs in the system;

  • Weak reference, as soon as the garbage collector finds it when it is working, it will be recycled immediately ;

  • Virtual reference is useless and may be recycled at any time.

In fact, unreachable objects determined by the reachability analysis method will not be recycled immediately. The object needs to be marked twice before it is actually recycled. . The first marking is to determine that the object is unreachable, and then a filter is performed. The filter condition is whether it is necessary to execute the finalize() method of this object. If the finalize() method is not overridden or the finalize() method has been called by the virtual machine, the finalize() method will only be called once by the system. In both cases, there is "no need to execute". If necessary, this object will be placed in the F-Quene queue, a low-priority automatically created by the virtual machine. FinalizerThe thread executes the finalize() method. During this period, the GC will mark the object in F-Quene for a second small scale. If the object is still not referenced, it will be recycled. Objects that are not filtered are not necessarily recycled.

#We already know what the object is Time has been recycled, so how to recycle it? Introducing the four most commonly used garbage collection algorithms:

  • Mark-clear: mark the objects that need to be cleared first, and then collect them uniformly ---- not efficient, will Generates a large number of discontinuous fragments;

  • Copy algorithm: Divide the memory into blocks, use only one block at a time, and copy the surviving objects to another after use. On one piece;

  • Marking and sorting: mark the surviving objects first, then move all surviving objects to one end, and directly clean up the memory outside the end boundary;

  • Generational algorithm, the heap is divided into the new generation and the old generation. A large number of objects will die every time the new generation is collected, so choose the copy algorithm. The survival rate of the old generation is relatively high, and there is no extra space for allocation guarantee, so choose the mark clearing or mark sorting algorithm.

The garbage collection algorithm is an idea of ​​memory recycling, and the specific implementation is a garbage collector. A brief introduction to commonly used garbage collectors:

  • serial serial collector. Single thread, other work must be suspended during garbage collection. Copying for new students, labeling for old people. Simple and efficient;

  • ParNew collector. The multi-threaded version of serial;

  • Parallel Scavenge collector, a multi-threaded collector of the replication algorithm. Pay attention to throughput, cpu running code time / total cpu time spent. New generation copy, old mark sorting;

  • Serial Old collector, old generation version;

  • Parallel Old collector, Parallel Scavenge old generation version;

  • CMS collector, focusing on the shortest pause. With a concurrent collector, the garbage collection thread works (basically) simultaneously with the user thread. Mark-and-sweep algorithm

For more details about the garbage collector, you can read Mr. Zhou Zhipeng’s book.

Recommended tutorial: "

JS Tutorial"

The above is the detailed content of A brief analysis of the heap and garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:nexttick principle in vueNext article:nexttick principle in vue