Get an insight into how the Java GC works _java

Source: Internet
Author: User
Tags garbage collection

JVM Learning notes the JVM memory management and the concept of JVM garbage collection, the JVM memory structure consists of heaps, stacks, local method stacks, method area, and so on, and the JVM used different garbage collection mechanism for the new generation download address and the old generation.

First take a look at the JVM memory structure, which consists of heaps, stacks, local method stacks, method areas, and so on, as shown in the chart below.

JVM Learning notes JVM memory management and JVM garbage collection

JVM Memory Composition Structure

The JVM memory structure consists of heaps, stacks, local method stacks, method areas, and so on, as shown in the following chart:

1) Heap

The memory of all objects created through new is allocated in the heap, and its size can be controlled by-XMX and-XMS. The heap is divided into cenozoic and old generations, and the Cenozoic is further divided into Eden and survivor, and the final survivor consists of Fromspace and tospace, the structure of which is shown as follows:

Cenozoic. New objects are used to allocate memory, Eden, when the space is insufficient, will be the survival of the object transferred to the survivor, the Cenozoic size can be controlled by-XMN, can also use-xx:survivorratio to control the proportion of Eden and survivor old generation. For storing objects that are still alive after multiple garbage collections in the Cenozoic.

2) stack

Each thread executes each method by requesting a stack frame in the stack, each of which includes a local variable area and an operand stack to hold the temporary variables, parameters, and intermediate results during this method invocation

3) Local Method stack

Used to support the execution of the native method, storing the state of each native method call

4) Method Area

The class information, static variables, final type constants, properties, and method information to be loaded are stored. The JVM uses a persistent generation (permanetgeneration) to hold the method area, and the minimum and maximum values can be specified by-xx:permsize and-xx:maxpermsize. After introducing the JVM memory composition structure, let's take a look at the JVM garbage collection mechanism download address.

JVM garbage collection mechanism

The JVM uses different garbage collection mechanisms for the Cenozoic and the old generations, respectively.

The new generation of GC:

The new generation usually has a short survival time, so based on the copying algorithm for recycling, the so-called copying algorithm is to scan the surviving object, and copied to a completely unused space, corresponding to the Cenozoic, that is, between Eden and Fromspace or tospace copy. The new generation uses idle pointers to control GC triggers, and the pointer retains the position of the last allocated object in the Cenozoic interval, and the GC is triggered to check if the space is sufficient when a novel object is to allocate memory. When the object is continuously allocated, the object is gradually from Eden to survivor, and finally to the old generation,

With JAVAVISUALVM to see, can obviously observe the Cenozoic after the full, will transfer objects to the old generation, and then empty continue to load, when the old generation is full, will report outofmemory Anomaly, as shown in the following figure:

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

1) Serial GC

The whole process of scanning and copying is done in a single-threaded way, which is applicable to single CPU, small Cenozoic space and not very high for pause time requirements, is the client-level default GC method, which can be enforced by-XX:+USESERIALGC to specify

2) Parallel Recovery GC

In the entire scanning and replication process in a multi-threaded manner, suitable for multiple CPUs, short pause time applications, is the server level by default GC method, can be-XX:+USEPARALLELGC to force the designation, with-XX: Parallelgcthreads=4 to specify the number of threads

3) Parallel GC

Used in conjunction with the old generation's concurrent GC

The GC of the old generation:

The old generation and the Cenozoic are different, the object survival time is relatively long, more stable, so using the mark (Mark) algorithm for recycling, the so-called mark is to scan out the surviving objects, and then to recycle the unmarked objects, after the recovery of the space to use out or merge, or marked out for the next time to allocate, In short, you want to reduce the loss of efficiency caused by memory fragmentation. The JVM provides serial GC (SERIALMSC), parallel GC (PARALLELMSC) and concurrent GC (CMS) on the execution mechanism, and details of the algorithm need further study.

The various GC mechanisms listed above are required to be combined, and are specified in the following table:

The above is small series for you to bring in-depth understanding of the working principle of the Java GC All content, I hope that we support cloud Habitat Community ~

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.