GC of Java Virtual machines

Source: Internet
Author: User

Main contents of garbage collection:

1. Which memory needs to be recycled?

2. When is it recycled?

3. How to recycle?

Garbage collection is primarily for those areas of the runtime data area?

The thread private areas of the runtime data area are: Virtual machine stack, local method stack, program counter, etc.

stack frames in the stack with the method entry and exit execution into the stack and out of the stack, the memory allocation of each stack frame in the compilation period has been determined;

As the thread or method ends, the memory is recycled as well;

The thread sharing area of the runtime data area is: Method area, heap;

The method area and the heap can only be determined when the program is running to determine which objects are created, so this part of memory allocation and recycling is dynamic;

Key areas of garbage collection;

One, the object survival judgment

1, reference counting algorithm

Adding a reference counter to an object, referencing the counter value +1, referencing the invalid counter value-1, when the counter value is 0 o'clock the object can no longer be used;

Mainstream Java virtual machines do not use this algorithm to manage memory (unresolved problems with circular references between objects)

Simple to achieve and high efficiency (application: Flashplayer,python, etc.)

2, the Accessibility analysis algorithm

The "GC Roots" object as the starting node, search down, search traversed by the path of the chain of reference, when an object to the GC Roots no reference chain, the object is not available;

Objects that can be used as "GC Roots":

"1", the object referenced by the static property in the method area

"2", the object referenced by the constant in the method area

"3", the object referenced by the virtual machine stack (local variable table in the stack frame)

"4", the object referenced by JNI in the local method stack (native method)

Second, garbage collection algorithm

1, mark-Clear algorithm

Definition: First mark the objects to be recycled, and then collect them uniformly;

Applicable: Garbage collection with many surviving objects

Disadvantages:

"1", the efficiency is low, the process of marking and cleaning is not high;

"2", space problem; After the mark is cleared, a large number of discontinuous memory fragments are generated, and there is not enough contiguous memory space to allocate memory to large objects, causing the garbage collection action to start early.

      

2, copy algorithm

Definition: Divide the available memory into two blocks of equal size, using only one piece at a time, and when this piece is exhausted, copies the surviving objects to another,

The used memory is then cleaned up once.

For: less-than-surviving garbage collection

Advantage: Each time the entire half of the memory collection, regardless of memory fragmentation problem, as long as the mobile heap top pointer, in order to allocate memory;

Easy to implement and efficient to run

Cons: Reduce memory by half

Other:

The Cenozoic memory is divided into Eden,from survivor,to Survivor three spaces according to 8:1:1, each time using Eden and from Survivor two spaces to allocate memory to the object,

When the memory is out of garbage collection, the surviving objects are copied to the to survivor space, and then the Eden and from survivor space are cleaned up, which equates to a 10% wasted memory point;

If the 10% to survivor space is not sufficient to store the surviving object, the old age is required for the allocation guarantee (the surviving object is directly into the old age through the distribution guarantee mechanism)

3, marker-collation algorithm

Definition: First mark the object to be reclaimed, move the surviving object to one end, and finally clear the memory outside the end boundary

      

4, generation of collection algorithm

Definition: The memory is divided into the Cenozoic and the old age according to the life cycle of the object, then the appropriate recovery algorithm is used according to the characteristics of each age;

such as: The new generation of surviving objects can be used in the replication algorithm; the old age has more surviving objects and no allocation guarantees. You must use the tag cleanup or mark-up recovery algorithm

Third, garbage collector

      

1,serial Collector

Definition: Single-threaded collector, must suspend all other user threads at collection time until the end of collection.

Application: New Generation

Configuration:

-xx:pretenuresizethreshold

-xx:handlepromotionfailure

Other:

"1", single CPU environment serial collector has no out-of-the-box interaction overhead, so single-threaded collection is the most efficient

"2", for desktop applications in client mode, the memory allocated to the virtual machine is not very large, for one hundred or two hundred trillion of the new generation of memory recovery pause time completely controlled in more than 100 milliseconds,

Pauses do not occur frequently, the serial collector is the best choice;

"3", the collection process will pause the service (Stop the World)

        

2,parnew Collector

Definition: is a multithreaded version of the serial collector

Application: New Generation

Configuration:

-xx:pretenuresizethreshold

-xx:handlepromotionfailure

-XX:+USECONCMARKSWEEPGC (set default Cenozoic collector)

-XX:+USERPARNEWGC (Specify Parnew as the new generation collector)

-xx:parallelgcthreads (Limit the number of threads that are garbage collected)

Other:

"1", the control parameters with the serial collector, the collection algorithm, the Stop the world, the object assignment rule, the recycling strategy exactly the same

"2" is the preferred Cenozoic collector for virtual machines running in server mode (only works with the CMS collector)

CMS is the concurrent collector, the first implementation of the collection thread and the user thread to work simultaneously;

CMS is an old age collector, unable to work with parallel scavenge;

The CMS focuses on the recovery pause time (pause user thread time), the shorter the pause time, the more suitable for the user interaction program, because has the high response speed

"3", single CPU environment without serial collector efficiency

"4", parallel multi-threaded collector

3,parallel Scavenge Collector

Definition: The same collector as the Parnew collector, different from the main focus on throughput control and GC adaptive adjustment strategy;

Note: throughput = Run user code time/(run user code time + garbage collection Time), if the virtual machine runs for 100 minutes and the collection takes 1 minutes, the throughput is 99%

Application: New generation; high throughput, resulting in high efficiency CPU utilization, primarily for background computing

Configuration:

-xx:maxgcpausemillis (maximum time to control recovery pauses; note: Smaller time will sacrifice throughput and Cenozoic space)

-xx:gctimeratio (Set throughput 0< X < 100; Garbage collection time as a percentage of total time, the reciprocal of throughput)

If Gctimeratio is set to 19, the maximum GC time is 5% of the total time (that is, 1/(1+19))

The value defaults to 99, and the maximum GC time is 1%

-xx:+useadaptivesizepolicy (Turn on GC adaptive throttling Policy)

                GC Adaptive Tuning Strategy: Virtual machine According to the current system performance, auto-tuning parameters have provided the most appropriate time and maximum throughput;

Parameters for adjustment include: ①, Cenozoic size (-xmn), Eden and survivor space Ratio (-xx:survivorratio)

②, age of promotion to the old age (-xx:pretenuresizethreshold)

... ...

GC Adaptive throttling policy gives memory management to virtual machines, only basic memory (-XMX), pause time (maxgcpausemillis), throughput (Gctimeratio) and other parameters to set the optimization target for the virtual machine;

Other:

"1" Parallel: multiple garbage collection threads working in parallel, single-user thread in wait state

Concurrent: The user thread and the recycle thread are concurrently

"2", parallel multi-threaded collector

      

4,serial Old Collector

Definition: is a single-threaded collector of the old age version of the serial collector

Applicable: The old age (marker-collation algorithm), mainly to the client mode of virtual machine use;

Other:

If there are two main uses in server mode: ①, use with parallel scavenge collector in JDK1.5 and previous versions

②, as a backup plan for the CMS collector (concurrent collection occurs when concurrent Mode failure is used)

                     

5,parallel Old Collector

Definition: A multi-threaded collector of the old age version of the parallel scavenge collector

Used: old age (marker-collation algorithm)
Other:

"1", JDK1.6 start to provide the collector

6,cms collector (Concurrent Mark Sweep)

Definition: CMS is a collection of concurrent collection, low pause

Focus on the goal: the shortest recovery pause time

Applicable: Internet station, b/S system service end (faster response speed, the shortest system downtime) better user experience

Algorithm: Tag-clear

Recycling steps:

"1" initial tag (the object to which the GC roots can be directly associated) (Stop the World)

"2" concurrency token (the process of GC Roots tracing)

"3" re-tagging (fixed tag record for objects that cause markup to change during a concurrent tag because the user program continues to run) (Stop the World)

"4" Concurrent cleanup

Execution Time:

T (concurrent token, concurrent purge) > t (re-tagging) > t (initial tag)

Because the longest-consuming concurrency token and concurrent cleanup work together with the user thread, the overall CMS recycling process is performed concurrently with the user thread.

Configuration:

-XX:+USECONCMARKSWEEPGC using the CMS collector

-xx:parallelcmsthreads set the number of threads for the CMS (typically approximately equal to the number of available CPUs)

Disadvantages:

"1" is very sensitive to CPU resources

The concurrency phase consumes a portion of the thread (CPU resources) that causes the application to slow down and the total throughput decreases.

CMS default number of recycle threads = (number of CPUs + 3)/4; Number of CPUs ↑ Recycle thread ↓; number of CPUs = 4 o'clock Recycle thread consumes 25%CPU resources;

The number of CPUs < 4 o'clock, such as the number of CPUs = 2, the recycling thread consumes half of the CPU resources, causing the user program to perform a direct decrease in the speed of the 50%,i-cms collector can solve this problem;

Incremental concurrency Collector (I-CMS): Enables GC threads and user threads to run alternately when concurrent tagging and concurrency cleanup, minimizing the time that GC threads monopolize resources

"2" cannot handle floating garbage, may appear concurrent Mode failure failure to produce full GC

Concurrent cleanup when the user thread is still running the garbage, because after the tag, the CMS can not be collected after the cleanup, only wait for the next GC cleanup, this part of the garbage is called "floating garbage";

Due to the inability to process floating garbage, these floating garbage occupy a certain amount of memory, but also consider to set aside enough memory for the user thread, so that the old age to provide reserved space;

So the CMS defaults to 68% when used to trigger a recycle, percent configuration (-xx:cmsinitiatingoccupancyfraction)

If the cmsinitiatingoccupancyfraction parameter is set too high, the concurrent Mode failure fails when there is not enough reserved space.

At this point, the virtual machine starts the serial old collector, and the garbage collection stops longer and the performance is degraded.

"3" Excessive space debris after recovery

Since the CMS adopts the tag-purge algorithm, it will generate a lot of space debris, and cannot allocate contiguous memory space for large objects to trigger full GC;

The +usecmscompactatfullcollection parameter is used to turn on the memory defragmentation process before triggering the full GC, which is not concurrent, so the pause time is extended.

The-xx:cmsfullgcsbeforecompaction parameter sets the full GC to be organized once, after how many times the full GC has not been collated.

        

7,G1 Collector

Definition: A garbage collector for service-side applications

Algorithms: Tagging-sorting

Characteristics:

"1" parallelism and concurrency

Take advantage of multi-CPU, multi-core environment to shorten the stop time of the world, use concurrent recovery to avoid GC pauses Java thread

"2" generational collection

"3" Space integration (using the tag-collation recovery algorithm to avoid the generation of large amounts of space memory fragmentation)

"4" Predictable pauses (G1 in the pursuit of low pauses while establishing a predictable pause time model that allows the user to set the time within m milliseconds for the GC to take no more than n milliseconds)

Principle:

The 1,G1 divides the Cenozoic and the old into independent regions of equal size, and carries out the whole area garbage collection, and the new generation and the old age are no longer physical isolation, and are the collections of some independent regions.

2, by calculating the value of the garbage accumulation in each region (the time required to reclaim the available space/recovery), and then prioritize the garbage collection according to the value, to ensure the efficiency of the recovery;

3, when a virtual machine discovers reference type data in a separate zone, it determines whether other independent regions have reference data references (that is, whether the older objects refer to the Cenozoic objects),

If you are logging the reference information through Cardtable to the remembered set of a separate zone, adding remembered set to the enumeration scope of the GC root node at garbage collection avoids using

The accessibility algorithm determines the whole heap scanning of the object's survival, and avoids the omission of the surviving object.

Recycling steps:

"1" initial tag (the object that the GC roots can directly relate to, and modifies the value of the Tams (next Top at Mark Start), allowing the next stage of the user program to run concurrently, creating the object in the correct region) (Stop the World)

"2" Concurrency token (starting from GC roots to analyze object accessibility in the heap to find surviving objects) (and user thread concurrency)

"3" final tag (fixed a tag record of the variable object that was run by the user thread during the concurrency tag, the virtual machine logs these objects into the remembered set logs, and then merges into the rembered set) (Stop the World)

"4" Filter Collection (value sequencing for each independent region, with a recovery plan based on the desired GC pause time)

      

Four, the use of the collector

1, parameters

USESERIALGC (using Serial + Serial Old combo collector)

USEPARNEWGC (using Parnew + Serial Old combo collector)

USECONCMARKSWEEPGC (using parnew + CMS + Serial old combo collector; Serial old is CMS appears concurrent Mode failure failed standby collector)

USEPARALLELGC (using parallel scavenge + Serial old combo collector)

USEPARALLELOLDGC (using Parallel scavenge + Parallel old combo collector)

Survivorratio (the ratio of Eden to Survivor capacity in Cenozoic; default is 8, i.e. Eden:survivor = 8:1)

Pretenuresizethreshold (direct promotion to the size of the old age object, which exceeds this parameter directly assigned in the old age)

Maxtenuringthreshold (age of promotion to the old age, each object minor after GC age +1, more than this parameter goes straight into the old age)

Useadaptivesizepolicy (dynamically adjusts the size of the Java heap and the age of entry into the old age)

Handlepromotionfailure (whether to allow the guarantee allocation to fail, that is, the remaining space in the old age is not sufficient to cope with the extreme situation where all objects in the new generation of Eden and Survivor are alive)

Parallelgcthreads (Sets the number of threads when parallel GC recycles)

Gctimeratio (GC time in total time ratio, default 99, which allows 1% GC time.) Only valid with parallel scavenge collector)

Maxgcpausemillis (set GC maximum pause time.) Only valid with parallel scavenge collector)

Cmsinitiatingoccupancyfraction (set CMS to trigger a GC after the old age space is used, default 68%, only with the CMS collector in effect)

Usecmscompactatfullcollection (set whether to make a memory defragmentation once the CMS finishes recycling, only with the CMS collector)

Cmsfullgcsbeforecompaction (set up CMS to start a memory defragmentation after several recoveries)

-xx:+printgcdetails (print virtual machine log)

2, combined use of the collector

        

Cenozoic Old generation Description
Combination 1 Serial Serial old are single-threaded for GC, and all application threads are paused while GC
Combination 2 Serial Cms+serial old CMS is concurrent GC, and the application thread concurrently works, does not need to pause all application threads CMS appears concurrent Mode failure fails after using serial old collector
Combination 3 Parnew Cms Using the-XX:+USEPARNEWGC option to turn on Parnew is a parallel version of serial, you can specify the number of GC threads, the default number of GC threads for the amount of CPUs using the-xx:parallelgcthreads option to specify the number of threads for GC-xx:+ USECONCMARKSWEEPGC option, the Parnew GC policy is used by default for the new generation.
Combination 4 Parnew Serial old Use the-XX:+USEPARNEWGC option to turn on
Combination 5 Parallel Scavenge Serial old The Parallel scavenge strategy focuses on a manageable throughput: application run time/(Application run time + GC time), which makes CPU utilization as high as possible for applications that run in the background, and not for applications that are more interactive.
Combination 6 Parallel Scavenge Parallel old Parallel old is a parallel version of serial old
Combination 7 G1 G1 -XX:+UNLOCKEXPERIMENTALVMOPTIONS-XX:+USEG1GC Open-xx:maxgcpausemillis = 50 pause time target-xx:gcpauseintervalmillis =200 Pause interval target-xx:+g1younggensize=512m The ratio between Eden and survivor capacity in the young generation-xx:survivorratio=6 Cenozoic

Java Virtual machine GC (RPM)

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.