How Java GC works and simple summary of minor GC, Major GC, full GC

Source: Internet
Author: User
Tags compact

Noun Explanation:

GC: Garbage collector

Minor GC: The new Generation GC, which is a new generation of garbage collection actions, all Minor GC triggers a worldwide pause (Stop-the-world), stopping the application thread, but this process is very short.

Major Gc/full GC: The old GC, referring to the GC that occurred in the old years.

Jvm:java the abbreviation for virtual machine (Java VM).


> Heap

As we all know, the memory of all objects created by new is allocated in the heap, the heap is divided into the Cenozoic and the old, and the Cenozoic is further divided into Eden and survivor area, while Survivor is composed of Fromspace and Tospace.

New generation: Newly created objects are used to allocate memory in the Cenozoic, when Eden space is insufficient, triggering the minor GC, the surviving objects will be transferred into the survivor area.

Old age: The old age was used to store objects that survived after multiple minor GC.

The structure diagram is as follows:

> Stacks

Each thread executes each method by applying a stack frame in the stack, each of which includes a local variable area and an operand stack for storing temporary variables, parameters, and intermediate results during the method call.

> Local Method stack

Used to support the execution of the native method, which stores the state of each native method call.

> method Area

Contains the class information to load, static variables, constants of the final type, properties, and method information. The JVM uses the Persistence generation (permanetgeneration) to store the method area.

The above is the JVM memory component structure.

Getting to the point: JVM garbage collection mechanism

The JVM uses different garbage collection mechanisms for the new generation and the old age respectively.

GC Trigger condition: The Eden area is full of triggering minor GC, this will copy the Eden area surviving objects to the Survivor area, when the object has survived a certain number of minor GC in the Survivor area, will be promoted to the old generation (of course not all the objects are so promoted to the old age), when the old age is full, will report outofmemory abnormal.

The new generation of GC (Minor GC):

The new generation usually has short survival time based on the copying algorithm for recycling, the so-called copying algorithm is to scan out the surviving objects, and copied into a completely unused space, corresponding to the Cenozoic, is between Eden and Fromspace or tospace copy. The Cenozoic uses a free pointer to control the GC trigger, the pointer keeps the last allocated object in the Cenozoic interval, and when a new object is allocated memory, it is used to check if the space is sufficient and not enough to trigger the GC. When objects are allocated continuously, the objects gradually go from Eden to Survivor, and finally to the old age.

The JVM provides a serial GC (SERIALGC), a parallel reclaim GC (Parallelscavenge), and a parallel GC (PARNEW) on the execution mechanism:

Serial GC

The entire scanning and copying process is a single-threaded way, suitable for single CPU, the new generation of space is small and the pause time requirements are not very high application, is the client level of the default GC mode, can be-XX:+USESERIALGC to enforce the designation.

Parallel Recycle GC

In the entire scanning and replication process in a multi-threaded way, for multi-CPU, the time required for a short pause on the application, the server level is the default use of GC mode, can be-XX:+USEPARALLELGC to enforce the designation, with-XX: Parallelgcthreads=4 to specify the number of threads.

Parallel GC

Used in conjunction with concurrent GC for the old age.

GC of the old age (Major gc/full GC):

The old age and the new generation, the old age object survived longer and more stable, so using the mark (Mark) algorithm for recycling, the so-called Mark is to scan out the surviving objects, and then to reclaim unmarked objects, after recycling the empty space is either merged, or marked out for the next allocation , the goal is to reduce the loss of efficiency caused by memory fragmentation.

The JVM provides a serial GC (Serial MSC), parallel GC (Parallel MSC), and concurrent GC (CMS) on the execution mechanism.

Serial GC (Serial MSC)

The default GC mode in client mode, which can be specified by-XX:+USESERIALGC. It is time-consuming to make the compact every time it is recycled.

Parallel GC (Parallel MSC) (throughput is large, but the GC responds very slowly)

The default GC mode in server mode can also be specified by-xx:+useparallelgc=. You can add an equal sign after the option to set the number of threads in parallel.

Concurrent GC (CMS) (response is much faster than parallel GC, but at the expense of a certain throughput)

The CMS is used to reduce the time that the GC executes while the garbage collection thread executes concurrently with the application thread, which can be specified using-xx:+useconcmarksweepgc=, followed by an equal sign to specify the number of concurrent threads. The CMS only pauses for a short period of time, respectively, at the beginning (Initial marking), and in the middle (Final marking), the second time is slightly longer. A larger problem with CMS is fragmentation and floating garbage issues (floating gabage). Fragmentation is due to the fact that the CMS does not compact the memory by default and can be-xx:+usecmscompactatfullcollection.

The virtual machine defines an object age counter for each object. If the object is still alive after Eden was born and after the first minor GC, and can be accommodated by survivor, it is moved to the survivor area and the object age is set to 1. Objects in the Survivor area each time minor GC, the age increases by 1, when its age increases to a certain extent (the default is 15), it will be promoted to the old age. The age threshold of an object's promotion to the old age can be set by parameter-xx:maxtenuringthreshold.

GC Determines whether an object is "alive" or "dead" (the object the GC recycles):

1. Reference counter algorithm

Adds a reference counter to the object, the value of the counter is incremented by 1 whenever there is a reference to it, the value of the counter is reduced when the reference is invalidated, and the object is invalidated when the value of the counter of the object is 0 o'clock.

2. Follow the search algorithm

Basic idea: Through a series of objects named "GC Roots" as the starting point, starting from these nodes down search, the search path is called the reference chain, when an object to the GC Roots no reference chain connected (in the case of graph theory, from the GC Roots to this object unreachable), It proves that the object is not available.

With JVM GC combination method:

Reprint Please specify: Li Feng's personal blog >>

How Java GC works and simple summary of minor GC, Major GC, full GC

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: 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.