JVM garbage collection problems

Source: Internet
Author: User

This article is from a blog with you. The original title is "JVM tuning Summary (4)-problems facing garbage collection".

The "reference count" method mentioned above is used to control the number of references when an object is generated or deleted. Garbage CollectionProgramCollect the objects with 0 counts. However, this method cannot solve the problem of circular reference. Therefore, the garbage judgment implemented laterAlgorithmAre all starting from the root node where the program is running, traversing the entire object reference and searching for surviving objects. In this way, where does 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 the memory, while the program running thread consumes (or allocates) the memory. One recycles the memory and the other allocates the memory. From 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.

However, this method has an obvious drawback: When the heap space continues to increase, the garbage collection time will also increase accordingly, the pause time of the application increases 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 problem, a concurrent garbage collection algorithm is used to run the garbage collection thread and the program running thread 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.

From: http://developer.51cto.com/art/200912/174321.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.