Java Programmer Develop Program JVM Learning notes (2)-garbage collection management

Source: Internet
Author: User

This article describes the JVM garbage collection, which includes three different algorithms (tag replication, tag cleanup, tag grooming), and seven different garbage collectors (serial,parnew,serial scavenge, CMS, Serial old, Parallel old, G1)
Continuous update ...

1. Garbage Collection Related Concepts 1.1. Garbage Collection Objects

When it comes to garbage collection, it's first to determine which objects are recyclable, and here are four references to Java: Strong, soft, weak, and virtual.

    • Strong reference: A universally available reference to an object, such as a pointer to an object created through new
    • Soft reference: A program that runs non-essential objects that are garbage collected only when a memory overflow is imminent during the program's run
    • Weak references: Weak references are also non-essential objects, but the difference from soft references is that once the garbage collector discovers the existence of a weak reference, it is recycled.
    • Virtual reference: You cannot get an object instance through a virtual reference, the existence of a virtual reference is basically just a matter of triggering a notification event before the object is garbage collected.
1.2. Pull Rubbish recovery area

The run-time data area has been briefly described in the previous section, where garbage collection is primarily involved in the area, that is, the heap area. Heap area is also subdivided into the new generation and the old age, is for the convenience of garbage collection, some do not often recycled or larger volume, the transfer of more troublesome objects in the old age, some of the objects will often be recycled into the new generation, and then different areas of the use of different garbage collector, can improve the efficiency of garbage collection.

1.3. Garbage Collection Object Determination

In general, determining whether an object instance should be recycled is primarily using the accessibility analysis algorithm, that is, traversing through a GC roots reference relationship to reach the object instance. Among them, the object that can be used as GC roots mainly consists of the following:

    • Objects referenced by static properties in the method area
    • Objects referenced by constants in the method area
    • Objects referenced by local variable tables in the virtual machine stack
    • Objects referenced by JNI (Java Native Interface) in the local method stack
2. Garbage collection Algorithm

Garbage collection algorithm is divided into three kinds, that is, Mark clear, tag copy, tag collation.

    • Mark Purge (Mark-sweep): Like its name, the tag cleanup algorithm first marks all objects that need to be recycled, and then clears this part out. This is a very simple algorithm, but there are two disadvantages, one is two steps is not very efficient, the other is very obvious will produce a lot of memory fragmentation. The tag cleanup algorithm is as follows:
    • "Tag Replication (mark-copying)": Tag replication is mainly to consider the markup cleanup algorithm will produce a large number of memory fragments of the algorithm proposed, specifically to divide the memory area into two blocks, each time there will be a piece will not be used, The algorithm runs by putting another object in the memory area in use into the block of memory that is not in use, and then clearing the original block once. The benefit is that there is no memory fragmentation and the implementation is simple and efficient. The disadvantage is that because a piece of memory can not be used, the utilization of memory can not reach all. The tag replication algorithm is as follows:
    • "Labeling Grooming (mark-compact)": Tagging takes into account both memory fragmentation and memory utilization, which, after the tag, moves the surviving objects so that the surviving objects are at one end of the memory and the other end is free memory. The tags are organized as follows:
3. Garbage collector

The garbage collector currently looks at 7 main types (serial,parnew,serial scavenge, CMS, Serial old, Parallel old, G1), from the new Laosheng generation division,:

Located above the young generation part of the new generation collector, located below the tenured generation part of the old age collector, where the connected collector represents these two can be used together.

3.1. Serial

The serial collector, which needs to stop all other threads from starting to collect garbage, uses a collection of tagged copies of the new generation of objects, and copies the surviving objects from the Eden and from survivor areas to the to Survivor area, if they are full.

3.2. Serial Old

Serial collectors, similar to serial, are merely collections of the old age, and the collection of the old age is marked by a collation.

3.3. Parnew

The new generation of parallel collectors, using the tag replication algorithm, is actually a multithreaded version of serial, enabling multithreading for collection during the garbage collection phase.

3.4. Parallel Scavenge

The new generation of parallel collectors, similar to Parnew, also uses the tag replication algorithm, but its focus is to make the CPU run user code time and total consumption time ratio is the throughput to a controllable amount, can control the throughput of a few percent, Then let the JVM determine the various GC and VM parameters on its own

3.5. CMS

The CMS, known as the concurrent Mark sweep collector, focuses on the shortest pause time, which gives the user the best experience. This collector is generally used as a markup cleanup algorithm, which is divided into four steps:

    • Initial tag: cannot be parallel to the user thread, but this phase only marks the object that GC roots can relate to
    • Concurrency token: This phase can be executed concurrently with the user thread, i.e., the accessibility monitoring along the GC roots tag
    • Re-tagging: this phase is primarily marked by those parts of the markup that have changed during the concurrency tagging phase due to the continued operation of the user thread, and this phase cannot be executed concurrently with the user thread.
    • Concurrent cleanup: This phase runs with the user thread, where the purge policy is used

From the steps of the CMS can be seen, its advantage is to reduce the user's pause time, to achieve a good user experience, the disadvantage is unable to handle floating garbage, floating garbage refers to the concurrent cleanup phase because the user thread is still running the resulting garbage. Because garbage collection is executed concurrently with the user thread, it is necessary to reserve some space for use by the user thread, not to start using it like any other collector until the memory is nearly full, the reserved size is implemented by configuration, and if the reserved space is too small, it can cause concurrent mode failure error, This time the serial old collector is enabled for garbage collection.
In addition, the CMS has a drawback is that the use of garbage removal algorithm, so there will be a lot of memory fragmentation, can be set to let the memory finishing process to a certain number of times after the fragmentation, integration process is not concurrent with the user thread, so the threshold setting also needs to be set according to the situation.

3.6. Parallel Old

That is, using parallel thread to garbage collection in the old age, using the labeling algorithm.

3.7. G1 (garbage first)

A significant difference between the G1 collector and other collectors is that garbage collection is no longer the entire new generation or the old age, and it divides the Java heap memory layout into a number of equally sized region. G1 tracks the garbage value of each region, that is, the amount of space and time it takes to reclaim the region, and then maintains a priority queue, recovering one of the most valuable region per time, based on the allowable collection times.
To avoid the garbage collection problems caused by the cross-referencing of each region, each region of G1 has a remembered Set that stores references to other region's objects in this region. When a memory is reclaimed, this part is also added when enumerating GC roots.
The steps for the G1 collector are as follows:
-Initial tag: marks the GC roots associated object, modifies the value of next Top Mark start, allowing the user thread to create objects in the correct region in subsequent concurrent executions.
-Concurrent tagging: Similar to CMS, the accessibility analysis begins with GC roots.
-Final markup: The remembered set Logs is generated during concurrent execution of the user thread, recording the change record of the object, which needs to be merged into the remembered set during the final marking phase.
-Screening and recycling: Evaluate the recovery value and cost of each region and make a recovery plan based on the GC time that the user wants to pause.

4. References

[1] "in-depth understanding Java Virtual Machine" Second Edition, Zhou Zhiming
[2] GC Other: Reference mark-Clear, copy, mark-organize instructions

Java Programmer Develop Program JVM Learning notes (2)-garbage collection management

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.