Garbage collection Policy in Java

Source: Internet
Author: User

Issues to be addressed in garbage collection
    • Who needs to be recycled
    • When is it recycled?
    • How to Recycle
Who needs to be recycled

If an object is never used again, it can be recycled, so the key is how to know that an object is no longer being used.

Reference count

When an object is referenced, the reference count is added to 1, and when the reference fails, the count is reduced by 1. Simple and intuitive, but there is a circular reference problem.

a   TB  =  b  b   ta  =  a  

Even if a and no b longer will be used, but they reference each other, resulting in a reference count has not been 0, can not be recycled.

Analysis of accessibility

Java, the mainstream implementation of C # is to use accessibility analysis to determine whether an object survives. If GC Root an object can be reached from, then the object is reachable, still alive, otherwise it will not survive, can be recycled. GC Rootcontains the following categories.

    • Objects referenced by the virtual machine stack
    • Object referenced by the method area static property
    • Objects referenced by constants in the method area
    • Objects in the local method stack that are referenced by JNI
When and how to recycle

When and how to recycle, involves the specific recycling strategy, put together said.

Tag Recovery algorithm

The most basic garbage collection algorithm, the first scan, marks all the objects that can be recycled, the second scan, and reclaims the tagged objects.

The shortcomings

    • Low efficiency
    • will generate memory fragmentation
Replication Algorithms

The replication algorithm divides the memory into two parts, using only two parts at a time. If the left half is currently used, the right half is not used. Now that you are garbage collected, copy the left half of the surviving objects to the right half of the block. The left half of the block is then recycled all at once.

Advantage is

    • Efficient
    • No memory fragmentation

The downside is

    • Space efficiency is low, half a block is not available

The workaround is to adjust the proportions of the left and right two blocks. In the HotSpot, the memory is divided into Eden, two Survivor, and the proportions are 8:1 . Use a piece of Eden each time, a Survivor A, and another Survivor B. During garbage collection, the surviving objects on Eden and Survivor A are copied to Survivor B, and Eden and Survivor A are recycled. Then the next time you use Eden, Survivor B, and Survivor A this time.

The premise of this approach is that a large number of objects are dead each time they are recycled, and only a small percentage of them are alive, so that only a small portion is copied. However, if a large number of objects survive longer, it is necessary to replicate back and forth, simply wasting life. It can also be seen from this point that different recycling strategies are used depending on the nature of the object's survival time.

Marker Grooming algorithm

The markup grooming algorithm is an improvement on the algorithm of Mark clearing. The first scan marks the objects that need to be recycled. The second time is not to purge these objects, but to move the surviving objects to one end of the memory area, all of them connected together. It is then recycled from all areas beyond the boundaries of the area.

Generational collection Algorithms

As you can see from the discussion above, different recycling strategies are used depending on the lifetime of the object. Some objects have a shorter survival time, so the replication algorithm can be used when there are fewer surviving objects per collection. Some objects have a longer time to live, so fewer objects need to be recycled each time they are recycled, and you can use the mark sweep and tag grooming algorithms. The Java heap is divided into the new generation and the old age, the object survival time is short in the Cenozoic, the object survival time is long in the old age.

There are two standard generations, one is survival time, the other is object size, and large objects will go straight into the old age.

HotSpot garbage collector

The HotSpot garbage collector can be divided into the new generation of recyclers and the old age collector. The number of garbage collector threads can be divided into single-threaded and multithreaded recyclers. Whether you need to stop all worker threads by garbage collection can be divided into concurrent and non-concurrent garbage collector. So, to see a collector, you need to understand it:

    • For the new generation or the old age.
    • Single thread or multithreaded
    • Do I need to stop all worker threads while working
Serial Recycle Collector
    • Default new Generation collector in Client mode with copy algorithm
    • Single Thread
    • Need to pause all worker threads
Parnew Recycle Collector
    • New Generation collector in Server mode with replication algorithm
    • Multithreading
    • No need to pause all worker threads
    • Only it can be used in conjunction with the CMS collector
Parallel Scavenge Recovery Device
    • Cenozoic collector, using the copy algorithm
    • multithreading
    • The goal is to achieve a manageable throughput (throughput = Run user code time/(run user code time + garbage Collection Time))

The other collector's goal is to reduce the time that the user program pauses due to garbage collection, while the parallel scavenge collector's target is the throughput that can be pressed because it is also called the throughput priority collector. There is a contradiction here, if each pause time decreases, then the user program can get a faster response, but it also means that garbage collection becomes frequent, garbage collection overall time is longer, the throughput is reduced. You can use to -XX:MaxGCPauseMillis adjust the garbage collection pause time, or you can use -XX:GCTimeRation adjust throughput.

Serial Old Collector
    • The old age collector, using the marker finishing algorithm
    • Single Thread
    • Need to pause all worker threads
    • Use in the Client mode
    • Serial Collector old age version
Parallel Old Collector
    • The old age collector, using the marker finishing algorithm
    • Multithreading
    • Parallel Scavenge old age version
CMS (Concurrent Mark Sweep) collector
    • Old-age collector, with tag-clearing algorithm
    • Multithreading
    • Overall, there is no need to pause all worker threads

Run the process:

    • Initial tag
    • Concurrency token
    • Re-tagging
    • Concurrent cleanup

Where concurrent tagging and re-tagging requires all user threads to be paused, these two phases are relatively rare in two phases. Concurrent tagging and concurrent cleanup consume a long time, but they can work with the user thread.

The initial tag marks only the objects that are directly associated with the GC Root and re-marks the object that corrected the tag changes during the concurrency token due to User program operation.

G1 (Garbage-first) collector
    • The new generation and the old age are no longer physically isolated, breaking the original generational concept
    • Multithreading
    • No need to pause all worker threads
    • Does not need to be used in conjunction with other garbage collector
    • Overall labeling algorithm, local replication algorithm, does not produce memory fragmentation
    • Can predict pauses, which means that the user can specify how long the recycle operation will complete

This article is a summary of the "in-depth understanding of Java virtual Machines".

Garbage collection Policy in Java

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.