Deep understanding of Java Virtual Machine notes---garbage collector

Source: Internet
Author: User

If the collection algorithm is the method of memory recycling, then the garbage collector is the specific implementation of memory recycling. In the Java Virtual Machine specification, how the object garbage collector should be implemented does not have any rules, so different vendors, different versions of the virtual machines provided by the collector may be quite different, and generally provide parameters for users according to their own application characteristics and requirements of the collection of the various years used. Here are all the collectors included with the Sun hotspot virtual Machine 1.6 release Update22:

, if there is a connection between the two collectors, it means they can be used in conjunction with each other.

1.Serial Collector
The serial collector is the most basic and oldest collector ever (before JDK1.3.1) is the only option for a new generation of virtual machines. This is a single-threaded collector, but its "single-threaded" meaning does not only mean that it uses only one CPU or one collection thread to do garbage work, but more importantly it does garbage collection, and must suspend all other worker threads (Sun calls this thing "Stop the World"), Until it collects the end. The following is the running process for the serial/serial old collector:

So far, the serial collection is the default re-generation collector that the virtual machine runs in client mode. It is simple and efficient (compared to the single thread of other collectors), and for a single CPU environment, the serial collector is naturally able to achieve the highest single-thread collection efficiency due to the lack of thread interaction overhead. In a desktop scenario, the memory allocated to the virtual machine is generally not too large to collect a few straight or even one hundred or two hundred trillion of new generation, the pause time can be completely controlled in dozens of milliseconds up to 100 milliseconds, as long as it does not occur frequently, this pause is acceptable. Therefore, the serial collector is a good choice for virtual machines running in client mode.

2.ParNew Collector
The Parnew Collector is a multithreaded version of the serial collector and, in addition to using multiple threads for garbage collection, the remaining behavior contains all the control parameters available to the serial collector (for example:-xx:survivorratio,-xx: Pretenuresizethreshold,-xx:handlepromotionfailure, etc.), collection algorithms, Stop the world, object assignment rules, and recycling policies are all identical to serial collectors. The working process of the Parnew collector is as follows:

3.Parallel Scavenge Collector
The Parallel scavenge collector is also a new generation collector and it also uses the Copy collection algorithm. This feature is different from other collectors ' concerns, such as CMS Collection Gaga focus is to minimize garbage collection when the user thread pause time, while the parallel scavenge collector is designed to achieve a controllable throughput (throughput). So swallowing is the ratio of the total CPU time consumed by the CPU to running user code, that is, throughput = Running user code time/(running user code time + garbage recovery time).
The shorter the pause time, the more suitable for the user to interact with the program, good response speed can improve the user experience, and high throughput can be the highest level of CPU time, the completion of the program task as soon as possible, mainly for the stepmother operation and do not need too much interaction tasks. The Parallel scavenge collector provides two parameters for precise control of throughput, respectively, to control the maximum garbage collection pause time-xx:maxgcpausemillis and to directly set the throughput size-xx:gctimeratio. Parallel Scavenge collector also has a parameter-xx:useadaptivesizepolicy, which is a switch parameter, when this parameter is opened, you do not need to manually specify the Cenozoic size, Eden and the ratio of the survivor area, Promotion of the age of the old age and other details of the parameters. The virtual opportunity collects performance monitoring information based on the current system's operation, dynamically invoking these parameters to provide the most appropriate pause time or maximum throughput, which is called a GC adaptive call policy (GC ergonomics).

4.Serial Old Collector
Serial old is an older version of the Serial collector, which is also a single-threaded collector, using the "mark-and-organize" algorithm. The main meaning of this collector is also used by virtual machines in client mode. In the case of server mode, it has two main purposes: one is used in conjunction with the parallel scavenge collector in JDK1.5 and previous versions, and the other is a backup plan as a CMS collector, where the concurrent collection occurs concurrent Mode Use when failure. The working process of the Serial old collector is as follows:

5.Parallel Old Collector
Parallel old is an older version of the Parallel scavenge collector, using multithreading and the "mark-and-organize" algorithm. This collector was only started in JDK1.6, and before that, the new generation of parallel scavenge collectors had been in a state of relative embarrassment. The reason is that if the new generation chooses the parallel scavenge collector, the older generation has no choice but to serial old (PS MarkSweep) collectors.
The working process of the Parallel old collector is as follows:

6.CMS Collector
The CMS (Concurrent Mark Sweep) collector is a collector that targets the shortest recovery pause time. At present, a large part of the Java applications are concentrated in the Internet or B/s system services, such applications pay particular attention to the response of the service, CMS collector is very suitable for this application needs.
The CMS collector is implemented based on the "tag-purge" algorithm, which is more complex than the previous collectors, and is divided into 4 steps, including the following:
A. Initial tag (CMS initial mark)
B. Concurrency token (CMS concurrent mark)
C. Re-tagging (CMS remark)
D. Concurrent Cleanup (CMS concurrent sweep)
Where the initial tag, re-tagging these two steps still need "Stop the World". The initial tag simply marks the object that the GC Roots can directly relate to, is fast, the concurrent tagging phase is the process of GC Roots tracing, and the re-tagging phase is to fix the concurrency tag period. Because the tag record of the part of the object that caused the tag to change as the program continues to run, the pause time of this phase is generally slightly longer than the initial marking phase, but is much shorter than the time of the concurrent tag.
Because the collector thread can work with the user thread during the longest concurrent markup and concurrent cleanup process throughout the process, the memory reclamation process for the CMS collector is, in general, executed concurrently with the user thread. The implementation diagram is as follows:

CMS is an excellent collector, but it has three notable drawbacks:
The A.cms collector is very sensitive to CPU resources.
The B.CMS collector cannot handle floating garbage (floating garbage), and a "Concurrent Mode Failure" failure may occur resulting in another full GC.
The C.cms collector uses the "tag-purge" algorithm, which results in a large amount of memory fragmentation.

7.G1 Collector
The G1 Collector is a product of the further development of the garbage collector theory, with two notable improvements compared to the previous CMS collector: The first is that the G1 collector is implemented based on the "mark-and-organize" algorithm, which means that it does not produce memory fragmentation, which is important for long-running application systems. The second is that it can control the pauses very precisely, that is, the use is explicitly specified in a time fragment length of M milliseconds, consumed in garbage collection time of not more than n milliseconds, which is almost a real-time Java (RTSJ) garbage collector features.
The G1 collector can achieve a low-pause memory recovery without sacrificing the amount of swallowing, because it is able to avoid the entire area of garbage collection, before the collector collects the whole new generation or the old age, and G1 the Java heap (including the new generation and the old age) Divided into a number of fixed-size independent areas, and tracking the level of garbage accumulation in these areas, maintain a priority list in the background, each time according to the allowable collection time, priority to reclaim the most garbage area (this is the origin of garbage first name). Regional division and Priority area recovery ensure that the G1 collector can obtain the highest collection efficiency in a limited time.

Deep understanding of Java Virtual Machine notes---garbage collector

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.