Types of garbage collector GC

Source: Internet
Author: User

Garbage collector is the implementation of garbage collection algorithm. The garbage collectors provided by different virtual machines can be quite different, and we are using all the collectors contained in the Hotspot,hotspot virtual machine

Shows 7 collectors that act on different generations, and if there is a connection between the two collectors, that means they can be used with each other. The area in which the virtual machine is located indicates whether it belongs to the new generation collector or the old collector. To say more, we must Yao Ming with a reason: there is no best garbage collector, there is no universal collector, can only choose the most suitable collector for the specific application. This is why the hotspot is implementing so many collectors. OK, one of the following looks at the collector:

1. Serial Collector

The most basic, longest-growing collector, a single-threaded collector with a copy algorithm, which means that it only uses one CPU or one thread to do garbage collection, and on the other hand, it must suspend all work of other threads when it is garbage collected. Until the end of its collection . The latter means that it is unacceptable for many applications to have all the threads of the user's normal work stopped when the user is not visible. But in fact, so far, theserial collector is still the default Cenozoic collector running in client mode for virtual machines because it is simple and efficient. In a user desktop scenario, the memory assigned to the virtual machine management is generally not very large, and collecting a few 10 trillion or even one hundred or two hundred trillion of new generation pauses in dozens of milliseconds for up to 100 milliseconds, which is perfectly acceptable as long as it does not occur frequently.

2. Parnew Collector

The parnew collector is actually a multithreaded version of the serial collector , with the exception of multiple threads for garbage collection, the same behavior as the serial collector, including the use of a copy algorithm. The Parnew collector does not have much innovation in addition to multithreading and the serial collector, but it is the new generation collector of choice for virtual machines in server mode, one of the important and performance-independent reasons is that, in addition to the serial collector, Currently only it can work with the CMS collector (see figure). The CMS collector is a garbage collector that can almost be considered epoch-making because it first implements a garbage collection thread that basically works with the user thread at the same time. The Parnew collector has absolutely no better effect than the serial collector in a single CPU environment, and even due to the overhead of thread interaction, the collector is not guaranteed to exceed the serial collector in two CPU environments. Of course, as the number of available CPUs increases, it is good for the efficient use of system resources in GC. It opens by default the same number of collection threads as the number of CPUs, and in the case of a very large number of CPUs, you can use the-xx:parallelgcthreads parameter to limit the number of threads that are garbage collected.

3. Parallel collector

The parallel collector is also a new generation collector, a collector with a copy algorithm, and a parallel multithreaded collector, but its focus is different from other collectors. This collector is mainly about the concept of throughput . The focus of collectors, such as CMS, is to minimize the downtime of user threads when garbage collection occurs, while the parallel collector's goal is to hit a controllable throughput . The so-called throughput means that the CPU is used to run the user code time and CPU total elapsed time ratio, that is, throughput = Run user code time/(run user code time + garbage collection Time), the total running of the virtual machine 100 minutes, garbage collection 1 minutes, the throughput is 99%. Additionally, theparallel collector is the default garbage collector that the virtual machine runs in server mode .

Short pause time for programs that need to interact with the user, good response speed can improve the user experience, high throughput can be efficient use of CPU time, as soon as possible to complete the operation of the task, mainly for the background operation and do not need too many interactive tasks.

The virtual machine provides-xx:maxgcpausemillis and-xx:gctimeratio two parameters to precisely control the maximum garbage collection pause time and throughput size. However, do not assume that the smaller the better, the shortening of the GC pause time is to sacrifice the throughput and the new generation of space exchange. Due to its close relationship to throughput, the parallel collector is also known as the throughput priority collector. The parallel collector has a-xx:+useadaptivesizepolicy parameter, which is a switch parameter that, when opened, does not need to manually specify details such as the Cenozoic size, Eden Zone, and survivor parameters, The virtual opportunity adjusts these parameters dynamically to provide the most appropriate pause time or maximum throughput based on the performance monitoring information of the mobile phone when the pro-system is running. If you are not familiar with how the garbage collector works, it is a good idea to give the memory management tuning task to the virtual machine by using the parallel collector with the adaptive throttling strategy when the optimization is difficult .

4. Serial Old Collector

The old version of the serial collector , which is also a single-threaded collector, uses the "mark-and-Organize algorithm", and the main meaning of this collector is to use the virtual machine in client mode.

5. Parallel Old Collector

The old version of the parallel collector, using multithreading and the "mark-and-organize" algorithm . This collector comes after JDK 1.6, and the "throughput priority collector" finally has a more veritable mix of applications that prioritize the combination of the Parallel collector +parallel old collector in the context of throughput and CPU resource-sensitive situations.

6. CMS Collector

The CMS collector is an old-age collector with the goal of obtaining the shortest recovery pause time . At present, a large part of the Java application focus on the Internet or B/s system services, such applications pay particular attention to the response speed of the service, hope that the system pause time is the shortest, in order to give users a better experience, CMS collector is very consistent with the needs of such applications. The CMS collector can be seen from the name as being implemented based on the "tag-purge" algorithm .

7. G1 Collector

The G1 (Garbage-first) collector is one of the most cutting-edge results of today's collector technology, and has been commercially available since JDK 7 Update 4. Other collectors before the G1 collector were collected in the entire new generation or the old age, and the G1 collector is no longer the case, the memory layout of the Java heap differs greatly from the other collectors when using the G1 collector, which divides the entire Java heap into separate, equal-sized regions (region). Although the concept of the new generation and the old age is preserved, the new generation and the old age are no longer physically isolated, they are a subset of the region. The G1 collector tracks the value of the garbage accumulation in each region, maintains a prioritized list in the background, and prioritizes recovering the most valuable region (which is also the origin of the Garbage-first name) each time it is allowed to collect. This uses the region to divide the memory space and has the priority area recovery method, guaranteed the G1 collector to obtain the highest collection efficiency in the limited time.

Garbage Collector Summary

Take a look at the summary of the garbage collector and make a list

GC Combination

    

    

Minor GC

      

      

Full GC

Describe
-xx:+useserialgc Serial Collector Serial Recycling Serial Old collector serial Recycle This option allows you to manually specify the Serial collector +serial old collector combination to perform memory reclamation
-xx:+useparnewgc Parnew Collector Parallel Recycle Serial Old collector serial Recycle This option allows you to manually specify the Parnew collector +serilal old combination to perform memory reclamation
-xx:+useparallelgc Parallel Collector Parallel Recycle Serial Old collector serial Recycle This option allows you to manually specify the parallel collector +serial old collector combination to perform memory reclamation
-xx:+useparalleloldgc Parallel Collector Parallel Recycle Parallel Old collector Parallel Recycle This option allows you to manually specify the Parallel collector +parallel old collector combination to perform memory reclamation
-xx:+useconcmarksweepgc Parnew Collector Parallel Recycle The default is to use the CMS collector for concurrent recycling, and the backup is serially recycled using the serial old collector

This option allows you to manually specify the Parnew collector +cms collector +serial The old collector combination to perform a memory reclamation. A combination of the Parnew collector +cms collector is preferred, and when concurrentmode fail or promotion failed is present, the Parnew collector +serial the combination of the old collector is used

-xx:+useconcmarksweepgc

-xx:-useparnewgc

Serial Collector Serial Recycling
-xx:+useg1gc G1 collector concurrency, parallel execution of memory reclamation Not currently

GC Log

The log form of each collector is determined by their own implementation, in other words, each collector's log format can be different. However, the virtual machine in order to facilitate the user to read, the various collectors of the logs have maintained a certain commonality, the most front of the object to reference the same class REFERENCECOUNTINGGC code as an example:

The virtual machine parameter is "-XX:+PRINTGCDETAILS-XX:+USESERIALGC" and the Log for garbage collection is used with the serial+serial old combination

[GC [Defnew:310k->194k (2368K), 0.0269163 secs] 310k->194k (7680K), 0.0269513 secs]
[times:user=0.00 sys=0.00, real=0.03secs] [GC [defnew:2242k->0k (2368K), 0.0018814 secs] 2242k->2241k (7680K), 0.0019172 secs]
[times:user=0.00 sys=0.00, real=0.00secs] [Full GC (System) [tenured:2241k->193K (5312K), 0.0056517 secs] 4289k->193k (7680K),
[perm:2950k->2950k (21248K)], 0.0057094 secs]
[times:user=0.00 sys=0.00, real=0.00secs] Heap defNewGeneration Total 2432K, used 43K [0x00000000052a0000, 0x0000000005540000, 0x0000000006ea0000) Eden Space 2176K,2% used [0x00000000052a0000, 0X00000000052AAEB8, 0x00000000054c0000) from Space 256K,0% used [0x00000000054c0000, 0x00000000054c0000, 0x0000000005500000) to space 256K,0% used [0x0000000005500000, 0x0000000005500000, 0x0000000005540000) tenured generation total 5312K, used 193K [0x0000000006ea0000, 0x00000000073d0000, 0x000000000a6a0000) The space 5312K,3% used [0x0000000006ea0000, 0x0000000006ed0730, 0x0000000006ed0800, 0x00000000073d0000) Compacting Perm gen Total 21248K, used 2982K [0x000000000a6a0000, 0x000000000bb60000, 0x000000000faa0000) The space 21248K,14% used [0x000000000a6a0000, 0x000000000a989980, 0x000000000a989a00, 0x000000000bb60000) No GKFX spaces configured.

The virtual machine parameter is "-XX:+PRINTGCDETAILS-XX:+USEPARNEWGC" and the Log for garbage collection is used with the parnew+serial old combination

[GC [parnew:310k->205k (2368K), 0.0006664 secs] 310k->205k (7680K), 0.0007043 secs]
[times:user=0.00 sys=0.00, real=0.00secs] [GC [parnew:2253k->31k (2368K), 0.0032525 secs] 2253k->2295k (7680K), 0.0032911 secs]
[times:user=0.00 sys=0.00, real=0.00secs] [Full GC (System) [tenured:2264k->194k (5312K), 0.0054415 secs] 4343k->194k (7680K),
[perm:2950k->2950k (21248K)], 0.0055105 secs] [times:user=0.00 sys=0.00, real=0.01secs] Heap parNewGeneration Total 2432K, used 43K [0x0000000005550000, 0x00000000057f0000, 0x0000000007150000) Eden Space 2176K,2% used [0x0000000005550000, 0X000000000555AEB8, 0x0000000005770000) from Space 256K,0% used [0x0000000005770000, 0x0000000005770000, 0x00000000057b0000) to space 256K,0% used [0x00000000057b0000, 0x00000000057b0000, 0x00000000057f0000) tenured generation total 5312K, used 194K [0x0000000007150000, 0x0000000007680000, 0x000000000a950000) The space 5312K,3% used [0x0000000007150000, 0x0000000007180940, 0x0000000007180a00, 0x0000000007680000) Compacting Perm gen Total 21248K, used 2982K [0x000000000a950000, 0x000000000be10000, 0x000000000fd50000) The space 21248K,14% used [0x000000000a950000, 0x000000000ac39980, 0x000000000ac39a00, 0x000000000be10000) No GKFX spaces configured.

The virtual machine parameter is "-XX:+PRINTGCDETAILS-XX:+USEPARALLELGC" and the Log for garbage collection is used with the parallel+serial old combination

[GC [psyounggen:4417k->288k (18688K)] 4417k->288k (61440K), 0.0007910 secs]
[times:user=0.00 sys=0.00, real=0.00secs] [Full GC (System) [psyounggen:288k->0k (18688K)] [psoldgen:0k->194k (42752K)] 288k->194k (61440K)
[pspermgen:2941k->2941k (21248K)], 0.0032663 secs] [times:user=0.02 sys=0.00, real=0.00secs] Heap Psyounggen Total 18688K, used 321K [0x0000000034190000, 0x0000000035660000, 0x0000000048f90000) Eden Space 16064K,2% used [0x0000000034190000,0x00000000341e05c0,0x0000000035140000) from Space 2624K,0% used [0x0000000035140000,0x0000000035140000,0x00000000353d0000) to space 2624K,0% used [0x00000000353d0000,0x00000000353d0000,0x0000000035660000) Psoldgen total 42752K, used 194K [0x000000000a590000, 0x000000000cf50000, 0x0000000034190000) object Space 42752K,0% used [0x000000000a590000,0x000000000a5c0810,0x000000000cf50000) Pspermgen total 21248K, used 2982K [0x0000000005190000, 0x0000000006650000, 0x000000000a590000) object Space 21248K,14% used [0x0000000005190000,0x0000000005479980,0x0000000006650000)

the virtual machine parameter is "-XX:+PRINTGCDETAILS-XX:+USECONCMARKSWEEPGC ", the Log for garbage collection using the parnew+cms+serial old combination

[Full GC (System) [cms:0k->194k (62656K), 0.0080796 secs] 4436k->194k (81792K),
[CMS perm:2941k->2940k (21248K)], 0.0081589 secs]
[times:user=0.01 sys=0.00, real=0.01secs] Heap parNewGeneration Total 19136K, used 340K [0x0000000005540000, 0x0000000006a00000, 0x0000000006a00000) Eden Space 17024K,2% used [0x0000000005540000, 0x0000000005595290, 0x00000000065e0000) from Space 2112K,0% used [0x00000000065e0000, 0x00000000065e0000, 0x00000000067f0000) to space 2112K,0% used [0x00000000067f0000, 0x00000000067f0000, 0x0000000006a00000) Concurrent Mark-sweep generation Total 62656K, used 194K [0x0000000006a00000, 0x000000000a730000, 0x000000000a940000) Concurrent-mark-sweep Perm Gen Total 21248K, used 2981K [0x000000000a940000, 0x000000000be00000, 0x000000000fd40000)

Some common features are extracted from the four GC logs:

1, the beginning of the log "GC", "full GC" represents the garbage collection of the type of pause, rather than to distinguish between the new generation GC or the old GC. If there is full, this GC stops all other worker threads. See that the full GC is written as "full GC (System)", which means that the GC triggered by the call to the System.GC () method is called.

2, "GC" in the Next "Defnew", "Parnew", "Psyounggen", "CMS" represents the new generation of garbage collector name, "Psyounggen" in the "PS" refers to "Parallel scavenge", It is the full name of the parallel collector.

3. In the first example, the "320k->194k (2368K)", "2242k->0k (2368K)" Inside the square brackets refers to the capacity that has been used in the memory area after the capacity->GC used in the area ( Total capacity of the memory area). The "310k->194k (7680K)", "2242k->2241k (7680K)" Outside the square brackets refers to the capacity that the Java heap has used (total Java heap capacity) after the capacity->GC the Java heap has been used before the GC.

4, also take the first as an example, and then "0.0269163 secs" indicates the time taken by GC in this memory area, in seconds. The final "[times:user=0.00 sys=0.00 real=0.03 secs]" is more specific, the user represents the CPU time consumed by the state, the CPU time consumed by the kernel state, and the clock wall time elapsed from the start to the end of the operation. The difference between the next two is that the clock wall time includes a variety of non-operational wait consumption, such as waiting for disk I/O, waiting for thread blocking, and CPU time does not include these times, but when the system has multiple CPUs or multicore, Multithreaded operations overlay These CPU times so it is perfectly normal for user or SYS to exceed real.

5. After "Heap", the memory of the region in the current age of heap memory is enumerated.

Timing of triggering GC

Finally, summarize when the GC is triggered, and in personal experience, there are three scenarios that trigger the GC:

1, the first scenario should be obvious, when the young generation or old age is full, the Java Virtual machine can no longer allocate memory space for new objects, then the Java virtual machine will trigger a GC to reclaim those objects that are no longer used

2. Manually invoke the System.GC () method, which typically triggers a full GC and at least one minor GC at a time

3, the program runs when there is a low priority GC thread, it is a daemon thread, when the thread is running state, naturally triggered a GC. This is also a good proof, but to use the knowledge of WeakReference, the back of the WeakReference will be devoted to this.

Types of garbage collector GC

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.