JVM Memory Management: Garbage collector detailed

Source: Internet
Author: User
Tags command line execution garbage collection thread

Introduction

In the previous chapter we have discussed the implementation of hotspot garbage collector, a total of six implementations of six combinations. This LZ together with you to discuss the six kinds of collectors of their respective power and the power of the combination.

In order to facilitate you to watch and contrast, LZ decided to use the original design pattern used in the way, for some collectors, divided into several dimensions to explain these collectors.

Client mode and server mode

Before introducing this chapter, let's talk about two patterns of the JVM, one in client mode and the other in server mode. The most important difference is that the mode used in our development is either the client mode or the command line parameter-server, and the biggest distinction is that the JVM does a lot of optimizations in server mode.

Java applications in server mode are slow to start, but because of the optimizations made by the JVM in server mode, the program will run faster and faster with long running hours. In contrast, Java applications in client mode start fast, but are not suitable for long runs, and can be significantly less performance than server mode if they are running for a long time.

The collector detailed

Let's look at the contents of a single garbage collector, and finally we'll talk a little bit about the characteristics of each combination.

Serial Garbage Collector

Algorithm: Using the replication algorithm

Memory Area: For the new generation design

Execution mode: Single thread, serial

Execution process: When the new generation of memory is not enough, pause all user programs, and then open a GC thread to use the replication algorithm to recycle garbage, there may be some objects raised to the older generation

Features: Because a single-threaded operation, and the entire GC phase to suspend the user program, it will cause the application to pause for a long time, but for small-scale programs, but very suitable.

Applicable scenario: Normal development and debugger use, and desktop application Interactive program.

Open parameters:-XX:+USESERIALGC (client mode default)

Serial Old Garbage Collector

Here the serial old collector no longer enumerates the features of each dimension because it is the same as the serial collector, the difference being that it is designed for older generations, and therefore uses a tag/collation algorithm. For the remaining dimension features, the serial old is identical to the serial collector.

Parnew Garbage Collector

Algorithm: Using the replication algorithm

Memory Area: For the new generation design

How to execute: multithreading, parallelism

Execution process: When the new generation of memory is not enough, pause all user programs, and then turn on several GC threads to use the replication algorithm for garbage collection in parallel, in the process of which some objects may be raised to older generations

Features: The use of multithreading in parallel, so the system will be more sensitive to the number of kernel processors, at least more than one processor, a few processors will open several threads (although the number of threads can be used-xx:parallelgcthreads=<n> control), Therefore, it is only suitable for multi-core multiprocessor systems. Although the user program is paused throughout the GC phase, multithreaded parallel processing does not cause too long pause time. So in terms of throughput, the parnew is larger than serial, and the more processors, the more obvious the effect. But this is not absolute, for a single processor, parnew performance will be lower than the serial collector due to the overhead of parallel execution, such as synchronization. Not only is a single processor, parnew performance is not necessarily higher than serial if you are on a smaller heap, or even two processors.

Scenario: On a medium to large heap with at least one system processor

Open parameter:-XX:+USEPARNEWGC

Parallel Scavenge Garbage Collector

This collector is almost exactly the same as parnew, and is a parallel collector for the new generation design, using the replication algorithm. The biggest difference with parnew is the different parameters that can be set, which allows us to control the GC pause time and throughput more precisely.

The parallel scavenge collector provides parameters that mainly include controlling the maximum pause time (using-xx:maxgcpausemillis=<n>) and controlling throughput (using-xx:gctimeratio=<n>). It can be seen from this that parallel scavenge is designed to provide throughput-controlled collectors.

But do not think that the maximum pause time to adjust the smaller the better, or the larger the throughput the better, in the use of parallel scavenge collector, there are three performance indicators, maximum pause time, throughput and the minimum value of the Cenozoic region.

The parallel scavenge collection appliance has the corresponding adjustment strategy, it will give priority to meet the maximum pause time target, the second is the throughput, the last is the Cenozoic region's minimum value.

Therefore, if the maximum pause time is adjusted too small, it will sacrifice the overall throughput and the new generation size to meet your selfish desires. The palm of the hand is meat, we'd better not do so. However, parallel scavenge has a parameter that allows the parallel scavenge collector to take full control of the size of the memory area, including the age of promotion to the older generation (which can use-xx:maxtenuringthreshold=n regulation), That is, use-xx:useadaptivesizepolicy to open the memory area size adaptive policy.

The parallel scavenge collector can be opened using the parameter-XX:+USEPARALLELGC and it is also the default Cenozoic collector in server mode.

Parallel Old Garbage Collector

The relationship between Parallel old and parnew or Parallel scavenge is like serial, and the difference between the two is small, except that the serial is a parallel collector designed for older generations, so it uses a marker /Collation Algorithm.

Another important significance of the Parallel old collector is that it is the only older generation collector that can work with the Parallel scavenge in addition to serial oldest, so in order to avoid serial The effect Parallel Scavenge can control the reputation of throughput, parallel old as parallel scavenge true partner.

It can be opened using the parameter-XX:-USEPARALLELOLDGC, but after JDK6, it is also the default old generation collector after opening the parallel scavenge.

Concurrent Mark Sweep Garbage Collector

Concurrent Mark Sweep (hereinafter referred to as CMS) collector is the only one that truly implements the application working with GC threads (together is for customers, not necessarily true together, there may be fast alternating) collectors.

The CMS is a collector designed for older generations and employs a tag/purge algorithm, and it is the only collector that uses the tag/purge algorithm in the older generation.

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.