Java Virtual machine 9: Garbage collection (GC)-4 (garbage collector)

Source: Internet
Author: User

1. Preface

Garbage collector is the previous chapter of the garbage collection algorithm theoretical knowledge of the specific implementation of the different virtual machines provided by the garbage collector may be a great difference, and we must explain in advance that there is no best garbage collector, more than a universal collector, can only choose the most suitable collector for the specific application. That's why the hotspot is implementing so many collectors, so let's take a hotspot as an example. Before writing, let's introduce a few concepts.

1.1. Differences in parallelism and concurrency

This distinction was introduced before in your special section, here again: These two nouns are concepts in concurrent programming, and in the context of talking about garbage collectors, you can understand these two nouns:

1. Parallel parallel

Multiple garbage collection threads work in parallel, but the user thread is still in a wait state.

2. Concurrent concurrent

The user thread executes concurrently with the garbage collection thread (but not necessarily in parallel, may be alternately executed), the user program continues to run, and the garbage collector runs on the other CPU.

The difference between 1.2.Minor GC and full GC

    • New Generation GC (Minor GC): Refers to the garbage collection action occurring in the new generation, because most Java objects have a characteristic of being out of date (survival rate is not high), so Minor GC is very frequent, the general recovery speed is relatively fast.

    • old age GC (Major gc/full GC): Refers to garbage collection actions occurring in the old age, Major GC, often accompanied at least once minor GC (but not absolutely, in parallel The scavenge collector's collection strategy has a direct major GC policy selection process). Major GC is typically 10 times times slower than the minor GC.

1.3. Throughput

Throughput: is the ratio of the CPU time spent running user code to the total CPU elapsed time, that is, throughput = Running user code time/(running user code time + garbage collection time). Virtual machines run for a total of 100 minutes, where garbage collection takes 1 minutes and the throughput is 99%.

2. Garbage collector Combination

The following diagram is a list of all the collectors that the hotspot virtual machine contains:

(A): The 7 different generations of collectors are shown in the figure:

Serial, Parnew, Parallel scavenge, Serial old, Parallel old, CMS, G1;

(B): And where they are located, they indicate whether they belong to the new generation collector or the old age collector:

Cenozoic Collectors : Serial, parnew, Parallel scavenge;

old age collector : Serial, Parallel, and CMS;

whole heap collector : G1;

(C): Two collectors are connected to each other, indicating that they can be used with :

Serial/serial old, Serial/cms, parnew/serial old, parnew/cms, Parallel scavenge/serial old, Parallel Scavenge/parallel Old, G1;

(D): where serial old as a CMS "Concurrent Mode Failure" Failure of the backup plan (described later);

2.1 Serial Collector

    1. Characteristics:
      The most basic, longest-growing collector, a single-threaded collector with a replication algorithm , single-threaded means that it only uses one CPU or one thread to do garbage collection, and on the other, it means that when it's garbage collected, All other worker threads must be paused until the end of the collection, and this process is also known as Stop the world. The latter means that it is obviously unacceptable for many applications to stop the user's normal working threads when they are not visible.

    2. Application Scenarios:
      The serial collector is still the default Cenozoic collector that the virtual machine runs in client mode. In the user's desktop scenario, the available memory is generally small (dozens of m to one hundred or two hundred m) and can be garbage collected (dozens of MS to more than 100 ms) in a short time, which is acceptable as long as it does not occur frequently

    3. Advantage:
      Simple and efficient (compared to the single thread of other collectors), for a single CPU-constrained environment, the serial collector is naturally able to achieve the highest single-thread collection efficiency due to the lack of thread interaction overhead. For example, in a user's desktop scenario, the available memory is generally small (dozens of m to one hundred or two hundred m) and can be garbage collected (dozens of MS to more than 100 ms) in a relatively short time, which is acceptable as long as it does not occur frequently.

Stop theworld Description:

GC in the background auto-initiated and automatic completion, in the case of the user is not visible, the user normal work threads all stop, that is , the GC pauses, will bring the user bad experience;

From JDK1.3 to now, from the serial collector-"parallel collector-" cms-"G1, the user thread pause time is constantly shortened, but still can not completely eliminate;

Setting parameters

"-XX:+USESERIALGC": Add this parameter to explicitly use the serial garbage collector;

The serial/serial old combo collector runs as follows:

2.2 Parnew Collector

    1. Characteristics:

      The Parnew Collector is a multithreaded version of the serial collector , in addition to using multiple threads for garbage collection, the rest behaves exactly like the serial collector, including all the control parameters available to the serial collector, the collection algorithm, the Stop the World, object allocation rules, and recycling strategies are all the same. Quite a lot of code is shared on the implementation.

    2. Application Scenarios:
      The parnew Collector is the preferred Cenozoic collector for many virtual machines running in server mode. The important reason is that, in addition to the serial collector, only it can work with the CMS collector (see figure). During the JDK1.5 period, hotspot launched a virtually epoch-making garbage collector-----cms collector , the first real-world concurrency collector in a hotspot virtual machine that For the first time, the garbage collection thread is implemented to work concurrently with the user thread .

    3. Advantage:
      In a single CPU environment, there is no better effect than the Serail collector, because of the thread interaction overhead, and even 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, which by default has the same number of collection threads as the number of CPUs.

Set parameters:

"-XX:+USECONCMARKSWEEPGC": After the use of CMS is specified, parnew is used as the new generation collector by default;

"-XX:+USEPARNEWGC": Forced to specify the use of parnew;

"-xx:parallelgcthreads": Specifies the number of threads that are garbage collected, parnew the collection thread that is turned on by default is the same as the number of CPUs;

The parnew/serial Old combo collector runs as follows :

2.3 Parallel Scavenge Collecting Device

    1. Characteristics:

      The Parallel scavenge collector is a new generation collector, it is also a collector using the replication algorithm, and is also a parallel multi-threaded collector.

    2. Comparative analysis:

      • Parallel scavenge collector VS cms collector:
        The Parallel scavenge collector is characterized by its focus on other collectors, where the focus of collectors such as CMS is to minimize the downtime of user threads when garbage collection occurs, while the Parallel scavenge collector's goal is to achieve a controlled throughput (throughput).
        Due to the close relationship with throughput, the Parallel scavenge collector is often referred to as the "throughput first" collector.

      • Parallel Scavenge collector VS parnew collector:
        An important difference between the Parallel scavenge collector and the Parnew collector is that it has an adaptive throttling strategy.

    3. Application Scenarios:
      The Parallel scavenge 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 collector targets high throughput, which is to reduce garbage collection time, thus allowing user code to run longer. So for applications that run on multiple CPUs and focus on background computing, such as performing batch processing tasks, order Processing, payroll payments, scientific calculations, and more.

Set parameters:

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.

"-xx:+maxgcpausemillis": Control the maximum garbage collection pause time, more than 0 milliseconds; the smaller the parameter setting, the slower the pause time may be, but it can also lead to reduced throughput, resulting in more frequent garbage collection.

"-xx:gctimeratio": Sets the ratio of garbage collection time to total time, 0<n<100 integer, which is equivalent to setting the size of the throughput.

The ratio of garbage collection execution time to application execution time is calculated by:

1/(1 + N)

For example, option-xx:gctimeratio=19, which sets the 5%--1/(1+19) of garbage collection time in total time;

The default value is 1%--1/(1+99), which is n=99;

The time spent on garbage collection is the total time collected by the younger generation and the old age;

GC Adaptive Tuning Strategy :

The Parallel scavenge collector has a parameter- XX:+UseAdaptiveSizePolicy . When this parameter is opened, there is no need to manually specify the size of the Cenozoic, the ratio of Eden to survivor, the age of the old age and other details, the virtual opportunity to collect performance monitoring information according to the current system operation, dynamically adjust these parameters to provide the most suitable time to pause or maximum throughput , this modulation is called the GC Adaptive Tuning Strategy (GC ergonomics). 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 .

Java Virtual machine 9: Garbage collection (GC)-4 (garbage collector)

Related Article

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.