Garbage collector in the JVM

Source: Internet
Author: User

The following collectors are used in the Cenozoic:

-xx:+useserialgc
-xx:+useparallelgc
-xx:+useparnewgc

The following collectors are used in the old age:

-xx:+useparalleloldgc
-xx:+useconcmarksweepgc



Serial collector

1, use in the new generation, the use of replication algorithm
2, it is a single-threaded collector, that is, it will only use a CPU or a collection thread to complete the garbage collection work.
3. When it is garbage collected, it must suspend all other worker threads until it collects the end



Parnew Collector

1, use in the new generation, the use of replication algorithm
2, it is the multi-threaded version of the serial collector


Parallel collector

1, the new generation of garbage collector, the same use of replication algorithm
2, parallel multi-threaded collection, this is basically consistent with the Parnew collector
3. Features (different points with parnew collector): Other collectors like CMS are designed to minimize the downtime of the user thread when garbage collection occurs, and the target of the parallel collector is to achieve a manageable throughput, throughput = Run user code time/(run user code time + Garbage collection Time)
4. Cannot be used in conjunction with the CMS collector



Serial Old Collector

1, used in the old age, using marker-collation algorithm
2. Single-Thread collector
3. When it is garbage collected, it must suspend all other worker threads until it collects the end


Parallel Old Collector

1, used in the old age, using marker-collation algorithm
2. Parallel multi-threaded collector
3, the same characteristics as the parallel collector
4, in the focus on the throughput of the occasion, you can consider using the Parallel collector +parallel old collector combination, one in the Cenozoic, one in the older era.


CMS (Concurrent mark-sweep Collector, concurrency Token-purge) collector

1. It is a collector with the goal of obtaining the shortest recovery pause time.
2, used in the old age, using the mark-clear algorithm



The entire process is divided into 4 steps:
Initial tag
Concurrency token
Re-tagging
Concurrent cleanup


Where the initial tag, the re-tagging still requires stop the world, the initial tag is just a token of the GC root directly associated with the object, fast, concurrent tagging is the process of GC Roots tracing. The re-tagging is to fix the tag record of the part of the object that caused the tag to change during the concurrent tagging period, which is usually slightly longer than the initial mark-up period, but is much shorter than the time of the concurrent tag. The process of memory reclamation for the CMS collector is performed concurrently with the user thread, as the most time-consuming concurrency token and purge-mark procedures throughout the process can work with the user thread.


Disadvantages:
1, the CMS is very sensitive to CPU resources, in fact, in the concurrency operation, although not cause the user thread to pause, but because of the use of a portion of CPU resources to cause the application to become slow.
2, unable to handle floating garbage, because the CMS cleanup phase and the user thread is parallel, so in the process of cleaning will also be a new garbage generation, this part of the garbage appears after the mark, so only the next GC processing, this part of the garbage is "floating garbage."
3, because the CMS uses the tag-clear algorithm, so it is easy to generate memory fragmentation.


What is the difference between serial and parallel collectors?
They lead to Stop-the-world during GC.
The serial collector has only one GC thread, which is collected by default using the replication algorithm.
The parallel collector uses multithreading for GC operations.


What is the difference between parallel and CMS collectors?
1. Parallel uses multithreading for GC operations, but the CMS has only one GC thread
2, parallel is a stop-the-world collector, CMS only in the initial marking and re-tagging phase is stop-the-world, in the concurrent tagging and concurrent cleanup phase is accompanied by the execution of the user thread.


If you want to combine parallelism and concurrency in GC, you can use-XX:USERPARNEWGC and-XX:+USECONCMARKSWEEPGC to combine.


G1 (Garbage-first) collector

The previous generation of garbage collectors (serial serial, parallel parallel, and CMS) divided the heap memory into three parts of a fixed size: young generation, older generation (old generation), and persistent generation (permanent Generation).



The G1 collector uses a different approach to managing heap memory.


In G1, the heap is divided into a number of contiguous regions (region). Each area is equal in size, between 1m~32m. The JVM supports up to 2000 zones, and the maximum memory supported by G1 can be estimated as 2000*32m=62.5g. The size of the region is determined by the JVM initialization, or it can be set with-xx:g1heapreginsize. In fact, these regions (regions) are mapped to logical Eden, Survivor, and old generation (older) spaces.

The colors in the diagram identify which role each region belongs to. When a GC is in progress, the surviving object is copied from one area to another. This area is designed to be garbage collected in parallel.

Each zone in the G1 has a corresponding remembered Set, which is used to store object references within the corresponding region, allowing for the parallel garbage collection between regions.


Collection process:

Initial tag
Concurrency token
Final tag
Filter Collection


There are many similarities with CMS, and the tagging phase simply marks the object that GC roots can directly relate to, which requires a stalled thread, but takes a short time.
The concurrency tagging phase begins with the GC root to perform a scalability analysis of objects in the heap, finding surviving objects, which take a long time, but can be executed concurrently with the user program. The final tag is to fix the tag record that is causing the markup to change as the user program continues to work during the concurrency tag, which requires a stalled thread and can be executed in parallel in multiple threads. Finally, the filter recovery stage first sorts the recovery value and the recovery cost of each region, and makes the recovery plan by the GC pause time expected by the user.


Garbage collector in the JVM

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.