Java Virtual machine detailed----garbage collector and GC parameters

Source: Internet
Author: User

"declaration"

Welcome reprint, but please keep the original source of the article →_→

Life One number: http://www.cnblogs.com/smyhvae/

Article Source: http://www.cnblogs.com/smyhvae/p/4748313.html

Contact information: [Email protected]

The main content of this article:

    • Review of the Heap
    • Serial collector
    • Parallel collector
    • CMS collector

0, Heap of review:

The new generation of 98% objects are "dying", so do not need to follow the 1:1 ratio of memory space, but the memory is divided into a larger Eden space and two smaller survivor space , Use Eden and one piece of survivor at a time. When recycled, the objects that are still alive in Eden and survivor are copied one at a time into another survivor space, finally clearing up Eden and the survivor space just used. the default Eden and survivor size ratios for the hotspot virtual machine are 8:1, meaning that the available memory space for each Cenozoic is 90% (80%+10%) of the total Cenozoic capacity, and only 10% of the space is wasted.

Of course, 98% of the objects can be recycled only in the general scenario of data, we have no way to ensure that only a few more than 10% of the object to survive, when the survivor space is not enough, we need to rely on the old age to allocate security, so large objects directly into the old age.

The structure of the heap is as follows:

Garbage collector:

The garbage collector is a concrete implementation of memory recycling when collecting algorithms for memory recycling.

Although we are comparing the various collectors, it is not to pick out the best collector. Since there is no best collector available until now, there is no universal collector, so we have chosen only the most appropriate collector for the specific application .

One, serial collector: Serial collector

    • The oldest and most stable
    • Simple and efficient
    • may result in longer pauses
    • -xx:+useserialgc

    New generation, older generations will use serial recycling

Cenozoic Replication algorithm

Old age Mark-finishing

Summary: Theserial collector is a good choice for virtual machines running in client mode .

This collector is a single-threaded collector, but its single-threaded meaning does not only mean that it uses a single CPU or a collection thread to complete garbage collection, but more importantly, when it is garbage collected, all other worker threads must be paused until it is collected. The collector runs as shown in the following procedure:

Second, parallel collector:

1. Parnew Collector:

    • The Parnew collector is actually the parallel version of the serial collector in the new generation.
    • Multi-threaded, requires multicore support.
    • -xx:+useparnewgc

Cenozoic Parallel

Old age Serial

    • -xx:parallelgcthreads limit the number of threads

2. Parallel Scanvenge Collector:

    • Similar to Parnew, but more focused on throughput
    • -XX:+USEPARALLELGC using parallel scanvenge collector: Cenozoic parallel, old generation serial

3. Parallel Old collector:

    • Parallel old Collector is an older version of the Parallel Scanvenge collector
    • -XX:+USEPARALLELGC using the parallel old collector: new generation parallel, older age parallel

As shown in the following:

Various parameter settings:

    • -xx:maxgcpausemills

Maximum pause time, per millisecond

GC tries to ensure that the collection time does not exceed the set value

    • -xx:gctimeratio

0-100 Range of values

Waste collection time as a percentage of total time

Default 99, which is the maximum allowable 1% time to do GC

Note: These two parameters are contradictory. Because the pause time and throughput cannot be tuned at the same time. One side of the buy hope that the time is low, and on the other hand the high throughput, in fact, this is contradictory. Because: in GC, the total amount of garbage collection is constant, if the pause time is reduced, the frequency will increase, since the frequency increases, the explanation will be frequent GC, the throughput will be reduced, performance will be reduced.

Throughput: The amount of time the CPU uses for user code/cpu the total elapsed time, that is, when the user code is running/(running user code time + garbage collection time). For example, virtual machines run for a total of 100 minutes, where garbage collection takes 1 minutes and the throughput is 99%.

Note 2: In all of the above collectors, when the GC is executed, the world will stop, but the following CMS collector will not.

Third, CMS collector:

The CMS collector (Concurrent Mark Sweep: concurrent Tag Cleanup ) is a collector that targets the shortest recovery pause time . Suitable for use in the Internet station or b/s system of the server, this kind of application of oil isolator attaches importance to the response of the server, I hope the shortest system pause time.

    • Concurrent Mark Sweep concurrent tag cleanup with low pauses
    • Tag-Purge algorithm
    • The concurrency phase reduces throughput (because the pause time is reduced, so the frequency of the GC becomes higher)
    • old age collector (new generation using parnew)
    • -XX:+USECONCMARKSWEEPGC Open this collector.

Note: Concurrency here refers to executing with the user thread.

2. CMS Collector Running Process: (emphasis on the implementation of the marking process)

(1) initial Mark

The object to which the root can be directly associated

Fast speed

(2) concurrency token (along with user thread)

Main tagging procedure, marking all objects

(3) re-tagging

The user thread is still running because of the concurrency token, so before the formal cleanup, make corrections

(4) concurrent cleanup (along with user threads)

Direct cleanup of objects based on tagged results

The entire process is as follows:

Where the initial tag and re-tagging are required stop the world.

The longest time in the process is concurrent tagging and concurrent purging, both of which can work with the user thread.

Examples of printing GC logs are:

3. Features of CMS collector:

(1) Minimizing pauses

(2) affects overall system throughput and performance

For example, in the user thread running process, half of the CPU to do GC, system performance in the GC phase, the reaction rate is reduced by half

(3) Not thorough cleaning

Because the user thread is still running during the cleanup phase, new garbage is generated and cannot be cleaned

(4) because it runs with the user thread, it cannot be cleaned when the space is almost full

-xx:cmsinitiatingoccupancyfraction setting thresholds for triggering GC

If the unfortunate memory space is not enough, it will cause concurrent mode failure

Let's take a look at the log of concurrent mode failure:

In case of encountering, we need to use serial collector as backup.

4. Since the tag cleanup algorithm can cause fragmentation of memory space, why does the CMS collector use the tag cleanup algorithm instead of using the tag grooming algorithm:

Answer:

The CMS collector pays more attention to the pause, which is working with the user thread when doing the GC (concurrent execution), and if the tagging algorithm is used, then the memory space of the available objects is moved when the cleanup is done, then the application's thread is likely to find no where the Application object is.

In order to solve the problem of fragmentation, the CMS collector will have some sorting parameters, and then this.

5, the various parameters when finishing:

    • -xx:+ usecmscompactatfullcollection

After the full GC, do a clean-up. The finishing process is exclusive, which causes the pause time to become longer

    • -xx:+cmsfullgcsbeforecompaction

Set up a few full GC to defragment once

    • -xx:parallelcmsthreads

Set the number of threads for the CMS

Four, the GC parameter collation:

-XX:+USESERIALGC: Using serial collectors in the new generation and in the old era

-xx:survivorratio: Setting the size of the Eden and Survivior extents

-xx:newratio: The ratio of the new generation to the old age

-XX:+USEPARNEWGC: Using parallel collectors in the Cenozoic

-XX:+USEPARALLELGC: The new generation uses the parallel collection collector

-XX:+USEPARALLELOLDGC: old age using the parallel collection collector

-xx:parallelgcthreads: Set the number of threads to use for garbage collection

-XX:+USECONCMARKSWEEPGC: New generation uses parallel collectors, older generations using cms+ serial collectors

-xx:parallelcmsthreads: Set the number of threads for the CMS

-xx:cmsinitiatingoccupancyfraction: Set the CMS collector to trigger after the old age space is used

-xx:+usecmscompactatfullcollection: Set whether the CMS collector will defragment the memory once the garbage collection is complete

-xx:cmsfullgcsbeforecompaction: Set the number of times the CMS garbage collection, after the memory compression

-xx:+cmsclassunloadingenabled: Allow recycling of class metadata

-xx:cmsinitiatingpermoccupancyfraction: When the occupancy rate of the permanent zone reaches this percentage, start the CMS recycle

-xx:usecmsinitiatingoccupancyonly: Only when the threshold is reached, the CMS is recycled

The final summary:

What do we need to be aware of in order to reduce GC pressure?

    • How the software Designs the architecture (the fundamental application of performance)
    • GC parameters are fine-tuned (unreasonable settings can affect performance, resulting in large delays)
    • How heap space is managed and allocated
    • How the code is written

Reference Links:

Http://www.cnblogs.com/zuoxiaolong/p/jvm8.html

Java Virtual machine detailed----garbage collector and GC parameters

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.