Java garbage Collection

Source: Internet
Author: User

Garbage collection Algorithm Reference count

Each object in the heap has a reference count, a reference count plus 1 when the object is referenced, and a reference count minus 1 when the object's reference is again assigned or out of the valid area, and when an object is recycled, the reference to the object it references is calculated minus 1. When the reference count of an object becomes 0 o'clock, it is recycled.

Advantages of reference Counting:

The garbage collector can run very quickly, and when an object has a reference count of 0 o'clock to reclaim the object, the garbage collection is interleaved in the program's normal operation without interrupting the program for a long time.

Disadvantages of reference counting:
    1. Adding and lowering of each reference count brings additional overhead
    2. Cannot detect a circular reference
Root Search algorithm

Garbage detection is achieved by establishing a collection of root objects (local variables, operands in the stack frame, objects referenced in local methods, constant pools, and so on) and examining the accessibility from which these root objects begin. The root object is always accessible, assuming that there is a reference path to an object from the root object, then it is called a reachable or active object, otherwise it is inaccessible and the inaccessible object is a garbage object.

Mark Clear

Divided into two stages of marking and clearing, during the tagging phase, the garbage collector tracks references from the root object, marks the object encountered during the trace, and finally the object that is not marked is the garbage object, in the purge phase, reclaims the memory occupied by the garbage object. The ability to add trace tags to the object itself, or to set the tag with a separate bitmap.

The markup cleanup method is the basic collection algorithm, and most of the other algorithms are aimed at improving the disadvantage of the algorithm.

There are two disadvantages:

    1. Efficiency
    2. Memory Fragmentation exists
Replication Algorithms

Dividing the memory into two regions of equal size, each time using only one of the regions, when the memory of the area is exhausted, the accessible objects are copied directly to the new area and stored continuously to eliminate memory fragmentation, when the accessible object is copied, the old memory area is cleared, and the referenced value is changed.

The disadvantage of such an algorithm is very obvious, can use memory into the original half, too wasted.

Under normal circumstances, most of the objects in the Cenozoic are very short-lived, that is, when garbage collection, most of the objects are garbage, only a small number of objects will survive, so just keep a small portion of memory to save the surviving objects, do not need to use half of the memory. In the Cenozoic, memory is generally divided into three parts: a larger Eden Space and two smaller survior spaces (the same size), each using Eden and a survior of memory, When you do garbage collection, you copy the surviving objects from Eden and the used Survior to a survior space, and then clear the memory for both spaces, and the next time you use Eden and a survior,hotspot, the scale of the three spaces is divided by 8. : 1:1, so the wasted space is only 1/10 of the total memory.

This partitioning of memory space is based on the existence of only a small number of objects if, for each garbage collection, most objects are garbage. If an exception is encountered, the object that survived in a garbage collection exceeds the total size of the reserved survior space, which relies on other memory allocations (for the collection, which is also described in the previous narrative as a new generation method).

Tag Grooming

Normal tag cleanup leaves memory fragmentation in memory, and the copy algorithm assumes that you don't want to waste 50% of memory to have a memory allocation guarantee, usually a memory generational, but there is always a generation that has no other guarantee on its behalf. The tagging process in the tagging algorithm is the same as tag cleanup, but the grooming part does not remove the garbage object directly, but instead moves the active object one end of the memory uniformly, then clears the memory area outside the boundary, thus avoiding the memory fragmentation. Does not waste memory, no other memory is required to guarantee

Collection of generations

Most of the objects created in most programs have a very short life cycle, and there will be a small number of objects with long life cycles, in order to overcome the disadvantage of copying all active objects per garbage collection in the replication collector, dividing the memory into different regions, and many others collecting the memory areas where the short life cycle resides. When an object undergoes a certain number of garbage collection to survive, it promotes its presence in the area. is usually divided into the new generation and the old age. The Cenozoic is also divided into Eden area, from Survior and to Survior.

Adaptive Collectors

Listen to the situation in the heap and call appropriate garbage collection techniques accordingly.

Garbage collector serial

A single-threaded collector, while garbage collection will suspend the work of other threads, not suitable for the server side of the virtual machine, but the client mode of the simulator is still able to use, because the client mode of the application allocated to the system memory is not generally small, garbage collection can be very fast completion. The advantage is simple and efficient, without the overhead of thread interaction, to get the highest single-thread collection efficiency.

Parnew

Seria multi-threaded version number, to be able to collect garbage multiple threads, but assuming that the CPU has only one core and no Hyper-threading, the effect is not necessarily better than serial, assuming that multicore or hyper-threading, can guarantee better results than serial, except Seria, This is the only garbage collector that can mate with the CMS collector

Parallel Scavenge

With the new generation of multi-threaded garbage collectors using the replication algorithm, the focus of the Parallel scavenge collector differs from other collectors, and the other collector's focus is to minimize the time that the user thread waits while the garbage collection is being collected, and Parallel The goal of the scavenge collector is to achieve a controllable throughput (throughput), which is the ratio of the CPU's time spent executing user code to the total CPU time consumed. Collectors that shorten the wait time for user threads are suitable for programs that need to interact with the user, whereas a throughput-targeted collector is suitable for tasks that do not require too much interaction with the user to target background operations.

Parallel Scavenge is able to set the time and throughput target for each garbage collection to be paused by the number of parameters, but the pause time is not as small as possible, at the expense of throughput and the new generation of space, due to the reduction of garbage collection pause time, only a small number of multiple collections can be or reduce the amount of space that needs to be collected.

Another-xx:useadaptivesizepolicy parameter, after specifying this parameter, does not need to manually specify the size of the Cenozoic, the proportions of the Eden and Survior areas, and the age of the old age, and so on, the virtual opportunity adjusts these parameters dynamically based on the information collected. , which is called adaptive policy.

Serial old

Serial's old version number, single-threaded collector, uses the "mark-and-organize" algorithm, primarily used by virtual machines in client mode, and has two uses when used in server mode:

    1. Used in conjunction with the parallel scavenge
    2. As an alternative to the CMS collection failure.
Parallel old

Parallel scavenge's old version number, using the "mark-tidy" algorithm, provided after the JDK1.6, assuming that the new generation chose Parallel scavenge, the old age can only choose serial, because serial Old is a single-threaded garbage collector and may affect collection performance. After the Parallel old appears, it is possible to choose Parallel scavenge and Parallel, respectively, in the Cenozoic and older generations.

CMS (Concurrent Mark Sweep)

To capture the shortest payback time for the collector, using the "mark-clear" algorithm, the entire recycling process is divided into the following 4 steps:

    • Initial tag (CMS Initial Mark)
    • Concurrent tagging (CMS current mark)
    • Mark once again (CMS Remark)
    • Concurrency Clear (CMS Concurrent Sweep)

The initial tag and another tagging phase still suspends execution of the user thread.

The initial tag is simply a record of the objects that the GC root can directly relate to, very quickly.

The concurrency token is the GC Roots tracing, which is slower, but can be executed at the same time as the user thread.

Another token is to fix the concurrency tag because the user thread execution caused the change of tag record, this phase will cause the user thread to pause, the pause time is slightly longer than the initial tag, but still less than the mark again.

Concurrent cleanup is the removal of garbage objects, which takes a long time, but can work with the user thread at the same time.

Disadvantages of the CMS
    1. Sensitive to CPU resources, concurrent phases and user threads executing at the same time, affecting server responsiveness, especially when CPU cores are few
    2. Floating garbage cannot be handled because the concurrent phase user thread is executing at the same time and may generate new garbage during the garbage collection process, because the CMS cannot process this part of the floating garbage because the user thread is executing at the same time during garbage collection and requires additional memory space, so it cannot wait until the memory is full for GC. There is a need to reserve a portion of space, assuming that this portion of space reserved is not enough GC when the user thread creates a new object to use, it uses the Prestaged method to perform a full GC with serial old.
    3. CMS is based on the "tag-purge" algorithm, there will be a memory fragmentation after garbage collection, when the application of large contiguous memory may be out of memory, there is a need for a full GC, you can specify after the full GC or how many times after the full GC memory compression to defragment memory.
G1 (Garbage first)

Based on the "mark-and-organize" algorithm, the problem of memory fragmentation is avoided, and the pauses in garbage collection can be controlled precisely.

The G1 collector is able to achieve a low-pause memory recovery without sacrificing throughput, unlike the previous garbage collector, where the recovery area of the G1 collectors is not the entire new generation or the old age, but instead divides the entire Java heap into fixed-size areas and tracks the level of garbage accumulation in these areas, Maintain a prioritized list in the background, prioritizing the most garbage-collected areas. Zoning makes each recovery time shorter, while priority partitioning enables the recovery of the largest amount of garbage per area, using the G1 collector to obtain the highest collection efficiency in a limited amount of time.

memory allocation and recovery policy

A minor GC (a Cenozoic Gc,major GC or full GC is an old-time GC) that takes precedence over the next generation of Eden, when the Eden Zone does not have enough memory

Large objects can allocate memory directly in the old age, can specify a size by the number of participants, and objects larger than this size allocate memory directly in the old age.

When minor GC is performed, the surviving objects in the Eden area and in a Survior area are copied to a Survior area where an object is added 1 each time it survives once in a minor GC. When this object is older than a certain value (default 15) It will enter the old age.

Assuming that objects of the same age in survior occupy more than half the space of Survior, objects older than or equal to that age will enter the old age without waiting for a certain age.

When the minor GC is performed, the average size of each promotion to the old age is greater than the size of the remaining space in the old age, assuming that the handlerpromotionfailure consent guarantee failure is turned on, assuming that the only minor GC is turned on. Otherwise, full GC is performed. Because the average size of the previous minor GC is used, it is assumed that one time the sudden size becomes larger, leading to the lack of space in the old age, that is, the guarantee fails and a full GC is performed again.

Finalize

When the GC is marked with the active object, the object that is not marked is a garbage object, but the garbage object is not cleared directly, and the garbage collector infers whether it needs to run the Finalize method of the object, assuming that the object does not overwrite the Finalize method or that its finalize has been run once. Then there is no need to run, otherwise it is necessary to run, when it is inferred that it is necessary to run, this object will be put into a f-queue queue, by a low-priority finalizer thread in the background to run the Finalize method of the object in the queue, Objects are able to resurrect themselves in this way, that is, once again referenced by other objects, but this function is only run by the garbage collector, and this function is no longer called when the object is reclaimed the second time. Later, the GC will run a second token against the objects in the F-queue queue.

Java garbage Collection

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.