Java Virtual Machine Learning-JVM Tuning Summary-issues facing garbage collection (8)

Source: Internet
Author: User

How to distinguish rubbish

The "reference counting" method mentioned above is judged by statistical control of the number of references to generate objects and delete objects. The garbage collector collects objects with a count of 0. However, this method does not resolve circular references. Therefore, in the garbage judgment algorithm that is implemented later, it starts from the root node of the program and traverses the whole object reference to find the surviving object. So where does garbage collection start in this way? That is, where to start looking for which objects are being used by the current system. The above analysis of the difference between heap and stack, where the stack is really the execution of the program, so to get which objects are being used, you need to start from the Java stack. At the same time, a stack is corresponding to a thread, so if there are multiple threads, then all the stacks corresponding to those threads must be checked.

At the same time, in addition to the stack, there are system runtime registers, etc., is also stored program running data. Thus, with references in the stack or register as the starting point, we can find the objects in the heap and find references to other objects in the heap, which are gradually extended to end with a null reference or a primitive type, thus forming an object tree that is the root node of the object referenced in the Java stack. If there are multiple references in the stack, it will eventually form multiple object trees. Objects on these object trees are objects that are required for the current system to run and cannot be garbage collected. Other remaining objects can be treated as objects that cannot be referenced, and can be recycled as garbage.

Therefore, the starting point for garbage collection is some root objects (java stacks , static variables , registers ... ). ). The simplest Java stack is the main function that the Java program executes. This method of recycling is also the "mark-clear" recycling method mentioned above.

How to deal with fragmentation

Since the time to live for different Java objects is not necessarily, fragmented memory fragmentation occurs after the program has been running for a period of time without memory grooming. The most immediate problem with fragmentation is the inability to allocate large chunks of memory space, and the efficiency of the program running down. Therefore, in the above mentioned basic garbage collection algorithm, "copy" and "Mark-tidy" way, can solve the problem of fragmentation.

How to resolve concurrent object creation and object reclamation issues

Garbage Collection Threads recycle memory, while program running threads consume (or allocate) memory, one reclaims memory, and one allocates memory , and from this point of view, the two are contradictory. Therefore, in the existing garbage collection mode, before the garbage collection, it is generally necessary to suspend the entire application (that is, suspend the allocation of memory), and then garbage collection, after the recovery is completed before continuing to apply. This realization is the most direct and most effective way to solve the contradiction.

However, there is a clear drawback of this approach, that is, when the heap space continues to increase, the time of garbage collection will continue to increase correspondingly, the corresponding application pause time will also increase correspondingly . Some applications that require a high amount of time, such as a maximum pause time of hundreds of milliseconds, are likely to exceed this limit when the heap space is larger than a few grams, in which case garbage collection becomes a bottleneck for the system to run. To resolve this contradiction, there is a concurrent garbage collection algorithm , which uses this algorithm to run the garbage collection thread concurrently with the program running thread. In this way, the problem of pausing is resolved, but because the object needs to be reclaimed at the same time as the newly generated object, the complexity of the algorithm will be greatly increased and the processing power of the system will be reduced correspondingly, and the "fragmentation" problem will be more difficult to solve.

Java Virtual Machine Learning-JVM Tuning Summary-issues facing garbage collection (8)

Related Article

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.