ParallelScavenge collector for JAVA Garbage Collector

Source: Internet
Author: User

ParallelScavenge collector for JAVA Garbage Collector

The Parallel Scavenge collector is a type of Garbage Collector in Java virtual machines. Similar to the ParNew collector, it is a new generation collector. The parallel multi-thread collector using the replication algorithm.

1. Features

The focus of the Parallel Scavenge collector is different from that of other collectors. The goal of the ParallelScavenge collector is to achieve a controllable Throughput (Throughput ). The so-called throughput is the ratio of the CPU time used to run user code to the total CPU consumption time, that is, the throughput = running user code time/(running user code time + garbage collection time ), virtual machines run for a total of 100 minutes, of which garbage collection takes 1 minute, And the throughput is 99%.

Due to the close relationship with throughput, the Parallel Scavenge collector is also often called the "throughput first" collector.

This garbage collector is the default value of Java Virtual Machine in Server mode. After Server mode is used, Java virtual machine uses Parallel Scavenge collector (New Generation) + Serial Old collector (Old generation) to recycle memory.

2. Use Cases

It is mainly suitable for background computing tasks that do not require too many interactions.

For example, if a program needs to interact with users, a good response speed can improve the user experience, while a high throughput can make the most efficient use of CPU time to complete program computing tasks as soon as possible.

3. Important Parameters

There are three important parameters, two of which are used to precisely control the throughput, namely the-XX: MaxGCPauseMillis parameter for controlling the maximum garbage collection pause time and the-XX: GCTimeRatio parameter. Another parameter is UseAdaptiveSizePolicy.

The value allowed by the MaxGCPauseMillis parameter is a millisecond value greater than 0, and the Collector will try its best to ensure that the memory reclaim time does not exceed the set value. However, we should not think that setting the value of this parameter a little smaller will make the garbage collection speed faster, the GC pause time is shortened at the expense of throughput and the new generation space in exchange: the system reduces the size of the new generation, and the collection of MB of the new generation is certainly faster than the collection of MB, this also directly leads to more frequent garbage collection. In the past, it was collected once every 10 seconds, and every pause was 100 milliseconds. Now it is collected once every 5 seconds, and every pause is 70 milliseconds. The pause time is indeed decreasing, but the throughput is also decreasing.

The value of the GCTimeRatio parameter should be an integer greater than 0 and less than 100, that is, the proportion of the garbage collection time to the total time, which is equivalent to the reciprocal of the throughput. If this parameter is set to 19, the maximum GC time allowed is 5% of the total time (1/(1 + 19). The default value is 99, that is, the maximum garbage collection time of 1% (1/(1 + 99) is allowed.

-XX: + UseAdaptiveSizePolicy is a switch parameter. When this parameter is enabled, you do not need to manually specify the ratio of the New Generation size (-Xmn), Eden, and Region VOR (-XX: Region vorratio) the object age in the old age (-XX: PretenureSizeThreshold) and other detailed parameters are promoted. The virtual opportunity Collects performance monitoring information based on the running status of the current system, dynamically adjust these parameters to provide the most appropriate pause time or maximum throughput. This adjustment method is called GC Ergonomics ).

4. Adaptive Adjustment Strategy

The Parallel Scavenge collector can work with adaptive adjustment policies to deliver Memory Management Optimization tasks to virtual machines. You only need to set the basic memory data (such as-Xmx setting the maximum heap), and then use the MaxGCPauseMillis parameter (more attention to the maximum pause time) or the GCTimeRatio parameter (more attention to the throughput) set up an optimization goal for the virtual machine, and the adjustment of specific parameters will be completed by the virtual machine. The adaptive adjustment policy is also an important difference between the Parallel Scavenge collector and the ParNew collector.

5. Example

Source code

Package com. gc;

Import java. util. ArrayList;

Import java. util. List;

/**

* Simple memory recovery for Java virtual machines and use of the Parallel Scavenge collector

* For running parameters, see the specific method. Note: You must enable the server mode before using it.

* @ Author fan fangming

*/

Public class EasyParallelScavenge {

Public byte [] placeHolder = new byte [64*1024]; // placeHolder

Public static voidmain (String [] args) throws Exception {

OutOfMemoryByExpansionSize ();

}

/**

* The size of the Java Virtual Machine can be expanded as appropriate, including Xms30m and Xmx400m.

* Argument:-server-Xms30m-Xmx100m-XX: + UseParallelGC-XX: + PrintGCDetails

* @ Author fan fangming

*/

Private static voidoutOfMemoryByExpansionSize () throws Exception {

List <EasyParallelScavenge> list = new ArrayList <EasyParallelScavenge> ();

While (true ){

EasyParallelScavengeserial = new EasyParallelScavenge ();

List. add (serial );

Thread. sleep (10); // pause for 10 ms

}

}

}

Parameter description:

-Server running

-Xms30m minimum Java VM memory 30 M

-Xmx100m Max Java VM memory 100 M

-XX: + UseParallelGC explicitly specifies the Parallel Scavenge collector

-XX: + PrintGCDetails Print Collection

The running result is as follows:

... [GC [PSYoungGen: 7449 K-> 3728 K (7552 K)] 66980 K-> 66980 K (75136 K), 0.0022792 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[GC [PSYoungGen: 7443 K-> 3728 K (7552 K)] 70695 K-> 70715 K (75136 K), 0.0027722 secs] [Times: user = 0.00 sys = 0.03, real = 0.00 secs]

[Full GC [PSYoungGen: 3728 K-> 3136 K (7552 K)] [PSOldGen: 66986 K-> 67563 K (76928 K)] 70715 K-> 70700 K (84480 K) [PSPermGen: 2088 K-> 2088 K (16384 K)], 0.0038774 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[GC [PSYoungGen: 3714 K-> 3744 K (7552 K)] 71278 K-> 71308 K (84480 K), 0.0017028 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[GC [PSYoungGen: 7459 K-> 3744 K (7552 K)] 75023 K-> 75025 K (84480 K), 0.0027427 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[GC [PSYoungGen: 7459 K-> 3744 K (7552 K)] 78739 K-> 78753 K (84480 K), 0.0048844 secs] [Times: user = 0.03 sys = 0.00, real = 0.01 secs]

[Full GC [PSYoungGen: 3744 K-> 1728 K (7552 K)] [PSOldGen: 75009 K-> 76922 K (87488 K)] 78753 K-> 78651 K (95040 K) [PSPermGen: 2088 K-> 2088 K (16384 K)], 0.0107597 secs] [Times: user = 0.00 sys = 0.00, real = 0.01 secs]

[GC [PSYoungGen: 3714 K-> 3744 K (7552 K)] 80637 K-> 80667 K (95040 K), 0.0015904 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[GC [PSYoungGen: 7459 K-> 3728 K (7552 K)] 84382 K-> 84372 K (95040 K), 0.0028244 secs] [Times: user = 0.03 sys = 0.00, real = 0.00 secs]

[GC [PSYoungGen: 7443 K-> 3728 K (7552 K)] 88087 K-> 88093 K (95040 K), 0.0023412 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[Full GC [PSYoungGen: 3728 K-> 640 K (7552 K)] [PSOldGen: 84364 K-> 87438 K (91072 K)] 88093 K-> 88078 K (98624 K) [PSPermGen: 2088 K-> 2088 K (16384 K)], 0.0048474 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[Full GC [PSYoungGen: 3714 K-> 128 K (7552 K)] [PSOldGen: 87438 K-> 91023 K (91072 K)] 91153 K-> 91151 K (98624 K) [PSPermGen: 2088 K-> 2088 K (16384 K)], 0.0047349 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[Full GC [PSYoungGen: 3714 K-> 3714 K (7552 K)] [PSOldGen: 91023 K-> 91023 K (91072 K)] 94738 K-> 94738 K (98624 K) [PSPermGen: 2088 K-> 2088 K (16384 K)], 0.0031963 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

[Full GC [PSYoungGen: 3714 K-> 3649 K (7552 K)] [PSOldGen: 91023 K-> 91070 K (91072 K)] 94738 K-> 94720 K (98624 K) [PSPermGen: 2088 K-> 2087 K (16384 K)], 0.0209529 secs] [Times: user = 0.03 sys = 0.00, real = 0.02 secs]

[Full GC [PSYoungGen: 3713 K-> 3713 K (7552 K)] [PSOldGen: 91070 K-> 91070 K (91072 K)] 94784 K-> 94784 K (98624 K) [PSPermGen: 2087 K-> 2087 K (16384 K)], 0.0035128 secs] [Times: user = 0.02sys = 0.00, real = 0.00 secs]

[Full GC [PSYoungGen: 3713 K-> 3713 K (7552 K)] [PSOldGen: 91070 K-> 91070 K (91072 K)] 94784 K-> 94784 K (98624 K) [PSPermGen: 2087 K-> 2087 K (16384 K)], 0.0027033 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

Exception in thread "main" java. lang. OutOfMemoryError: Java heap space

Atcom. gc. EasyParNew. <init> (EasyParNew. java: 12)

Atcom. gc. EasyParNew. outOfMemoryByExpansionSize (EasyParNew. java: 39)

Atcom. gc. EasyParNew. main (EasyParNew. java: 14)

Heap

PSYoungGen total 7552 K, used 3776 K [0x0e4b0000, 0x0efc0000, 0x0efc0000)

Eden space 3776 K, 100% used [0x0e4b0000, 0x0e860000, 0x0e860000)

From space 3776 K, 0% used [0x0ec10000, 0x0ec10000, 0x0efc0000)

To space 3776 K, 0% used [0x0e860000, 0x0e860000, 0x0ec10000)

PSOldGen total 91072 K, used 91070 K [0x08bc0000, 0x0e4b0000, 0x0e4b0000)

Object space 91072 K, 99% used [0x08bc0000, 0x0e4af980, 0x0e4b0000)

6. Simple differences between the new generation collectors serial and parNew

[GC [DefNew: 1986 K-> 128 K (2112 K), 0.0011191 secs] 27809 K-> 27808 K (30528 K), 0.0011425 secs] [Times: user = 0.00sys = 0.01, real = 0.00 secs]

And

[GC [ParNew: 1990 K-> 132 K (2112 K), 0.0007742 secs] 24112 K-> 24110 K (30528 K), 0.0007964 secs] [Times: user = 0.00sys = 0.00, real = 0.00 secs]

And

[GC [PSYoungGen: 7449 K-> 3728 K (7552 K)] 66980 K-> 66980 K (75136 K), 0.0022792 secs] [Times: user = 0.00 sys = 0.00, real = 0.00 secs]

GC [ParNew indicates that the parNew collector is used.

GC [DefNew indicates that the serial collector is used.

[GC [PSYoungGen indicates that the Parallel Scavenge collector is used.

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.