Home >Web Front-end >JS Tutorial >A brief analysis of the heap and garbage collection mechanism
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;
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:
##Reference counting method
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.
#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.
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!