JVM Tuning Summary (iv)-Problems faced by garbage collection

Source: Internet
Author: User

How to distinguish rubbish

The reference count method mentioned above is used to determine the number of references to generate objects and delete objects by statistical control. The garbage collector collects objects with a count of 0. However, this method does not resolve the circular reference. Therefore, the later implementation of the garbage detection algorithm, are from the program running root node, traversing the entire object reference, to find the surviving object. So where does garbage collection begin in this way of implementation? That is, where to start looking for which objects are being used by the current system. The difference between the heap and stack analyzed above, where the stack is really where the program executes, so to get which objects are being used, you need to start from the Java stack. Also, a stack corresponds to a thread, so if you have more than one thread, you must check all the stacks that correspond to those threads.

At the same time, in addition to the stack, there are system runtime registers and so on, but also stored programs running data. In this way, the reference in the stack or register is the starting point, we can find the objects in the heap and then find references to other objects in the heap, which are gradually extended to end with null references or base types, thus forming an object tree with the root node of the object corresponding to the reference in the Java stack. If there are multiple references in the stack, a number of object trees will eventually form. Objects on these object trees are objects that are required for the current system to run and cannot be garbage collected. Other remaining objects, then, can be treated 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 that the Java program executes. This type of recycling is also the "tag-erase" recycling method mentioned above

How to handle fragmentation

Because the different Java objects are not necessarily alive, so, after the program runs for a period of time, if no memory collation, there will be fragmented memory fragmentation. The most immediate problem with fragmentation is the inability to allocate large chunks of memory space and the inefficient operation of programs. So, in the basic garbage collection algorithm mentioned above, the "copy" method and the "tag-organize" approach can solve the problem of fragmentation.

How to resolve simultaneous object creation and object recycling issues

Garbage collection threads are reclaimed memory, while program running threads are consuming (or allocating) memory, a reclaimed memory, an allocation of memory, from this point of view, the two are contradictory. Therefore, in the existing garbage collection, before the garbage collection, the general need to suspend the entire application (that is, pause 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 resolve the contradictions between the two.

But this way has a very obvious disadvantage, that is, when the heap space continues to increase, garbage collection time will correspondingly increase, the corresponding application pause time will correspondingly increase. Some applications that require high time requirements, such as a maximum timeout requirement of hundreds of milliseconds, are more likely to exceed this limit when heap space is greater than a few g, in which case garbage collection becomes a bottleneck in the system's operation. To solve this contradiction, there is a concurrent garbage collection algorithm, using this algorithm, the garbage collection thread and the program running thread run concurrently. In this way, the problem of pause is solved, but because the need to reclaim the object at the same time as the new object, the complexity of the algorithm is greatly increased, the processing power of the system will be reduced correspondingly, and the "fragmentation" problem will be more difficult to solve.

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.