How to differentiate garbage
The "reference count" method mentioned above is used to control the number of references when an object is generated or deleted. The garbage collection program collects zero objects. However, this method cannot solve the problem of circular reference. Therefore, the subsequent garbage judgment algorithms start from the root node where the program runs and traverse the entire object reference to find the surviving object. In this way,Where did garbage collection start?? That is, where to find which objects are being used by the current system. The difference between the heap and stack analyzed above, where the stack is the place where the program is actually executed, so to obtain which objects are being used, you need to start from the java stack. At the same time, a stack corresponds to a thread. Therefore, if there are multiple threads, all the stacks corresponding to these threads must be checked.
In addition to the stack, there are also system runtime registers and so on, which also store program running data. In this way, starting from the reference in the stack or register, we can find the objects in the heap and reference other objects in the heap from these objects. This reference is gradually extended, end with a null reference or basic type. In this way, an object tree with the corresponding objects referenced in the java stack as the root node is formed. If there are multiple references in the stack, multiple object trees are formed. Objects on these object trees are the objects required for the current system operation and cannot be recycled. Other remaining objects can be regarded as objects that cannot be referenced and can be recycled as garbage.
Therefore,The starting point of garbage collection is some root objects (java stack, static variables, registers ...). The simplest java stack is the main function executed by the Java program. This recycling method is also the "mark-clear" recycling method mentioned above.
How to handle fragments
Because different Java objects do not survive for a certain period of time, after a program runs for a period of time, if you do not perform memory sorting, there will be scattered memory fragments. The most direct problem of fragmentation is that it will lead to the failure to allocate a large block of memory space and reduce the program running efficiency. Therefore, in the above-mentioned basic garbage collection algorithm, the "copy" and "tag-sort" methods can both solve the fragmentation problem.
How to solve the problem of simultaneous object creation and object recycling
The garbage collection thread recycles memory, while the program running thread consumes (or allocates) memory,One memory recovery and one memory allocationFrom this point of view, the two are in conflict. Therefore, in the existing garbage collection method, the entire application must be suspended (that is, the memory allocation should be suspended) before garbage collection, the application will continue after the collection is completed. This implementation method is the most direct and most effective way to solve the conflict between the two.
HoweverThe obvious drawback of this method is that when the heap space continues to increase, the garbage collection time will also increase accordingly, and the pause time of the corresponding application will also increase accordingly.. For some applications with high time requirements, for example, the maximum pause time must be several hundred milliseconds. When the heap space is larger than several GB, it is likely to exceed this limit. In this case, garbage collection will become a bottleneck in system operation. To solve this conflictConcurrent garbage collection AlgorithmUsing this algorithm, the garbage collection thread and the program running thread run simultaneously. In this way, the pause problem is solved, but because the object needs to be recycled while being generated, the complexity of the algorithm will be greatly increased, and the processing capability of the system will be reduced accordingly, at the same time, the "Fragmentation" problem will be more difficult to solve.
JVM optimization Summary (4) problems faced by garbage collection