JVM memory model and Hotspot's GC policy

Source: Internet
Author: User
Tags compact

Overview

To learn more about the Java language, it is important to understand the running environment of Java programs. This article will approximate the JVM's memory model, the Java Memory Auto-management mechanism, and the implementation strategy of the Oracle official virtual machine hotspot in GC.

What is the memory model of Java?

As is known to all, Java programs run on the JVM, but do not specify which JVM, as long as a certain specification of the JVM, can correctly run Java bytecode. The specification, provided by Oracle, is intended to describe an abstract JVM that contains many parts, including the class file structure, runtime memory state, instruction set, and so on.

This part of the run-time memory state described above is often referred to as the JVM (Java) memory model. It mainly describes how the various types of data are presented in memory at runtime. The JVM memory model does not specify how the rendering should be implemented, but instead specifies some logically-owned modules, as well as information about the scope, size, and type of data stored for each module.

What does the Java language-defined memory model look like?

The JVM specification gives the runtime's memory model, some modules that should be included, and the characteristics of each module, as shown in:

What is a GC?

Commonly referred to as GC, can be called garbage Collection or garbage COLLECTOR,GC both indicate a memory management mechanism, can also be seen as a memory management program, here do not make a detailed distinction. One of the big differences between Java and C + + is the automatic memory management mechanism. Since memory is automatically managed, it involves the allocation and recovery of memory. and is automatic, so when the Java program runs, there must be a program in the JVM that is responsible for doing this, which is the GC.

GC has its own responsibilities, mainly with the following responsibilities:

Allocate Memory -find enough memory space for new object allocation requests.

Reclaim garbage Memory -when memory is full, you cannot allocate memory for new objects, or when memory usage reaches a certain percentage, reclaim objects that are no longer in use (Garbage objects).

Defragment memory -after the reclamation is complete, there may be a lot of discontinuous memory space called memory fragmentation. Defragmenting is useful for allocating memory for new objects faster next time.

What is memory garbage?

Java is an object-oriented language, and normal is the object of operation, then when an object is exhausted, it is no longer necessary to use, it becomes a memory garbage. In the JVM, garbage objects are identified primarily through GC roots. When an object, starting from any GC roots, cannot access the object, the object is a garbage object.

The GC roots is defined as follows:

Class-system ClassLoader loaded class, these classes cannot be unloaded. They can hold objects through static variables. Custom ClassLoader are not GC Roots.

thread -the thread that also survives.

JVM Stack -parameters for local variables or methods.

The local variable or parameter of the JNI local-native method.

JNI Global- a global JNI reference.

Held by JVM -System ClassLoader, a small subset of the JVM knows important exception, some pre-allocated objects for handling exceptions, and custom classloader that are loading class.

How to measure the performance of a GC? Production capacity-The percentage of time that the application runs and the total elapsed time. additional consumption of GC runs-The percentage of time that the GC program runs and the total elapsed time. Pause Time-The time that the application was paused while the GC was running. GC Frequency-Frequency of GC execution size of covered area-The size of the area where the GC works. the timeliness of garbage collection-an object becomes a garbage object to the time of being reclaimed.

How does a hotspot implement a GC strategy?

The hotspot uses a generational management approach. Different memory areas of the object, the age of survival is not the same.

1. New Generation (young generational)

Most objects are directly allocated to the new generation of Eden areas (some large objects are directly allocated in the old age), and the from region of Survivors preserves those who survived at least one new generation of GC, but cannot be considered "old enough" to have more chances of dying. And the to area is empty. The two regions alternate to transform identities (the next GC, now the to region becomes the from zone).

2, old generational

Contains objects that survived from the Cenozoic, as well as some large objects that were directly allocated in the old age,

3. Permanent generation (permanent generational)

Contains the GC collector of those JVMs to facilitate manageable objects, such as the description object of a class or method, and the class and object itself. Method area and Run-time Constant Pool.

What are the GC types? 1. Minor collection (Young generational collection)

trigger Condition: When the Cenozoic region is full, it will trigger.

Recycling Area: Only the new generation is recycled.

2, full collection (major collection)

trigger Condition: when the old age or the persistent generation area is full, it triggers.

Reclaim Area: Three areas will be recycled.

What garbage Collector are there in the hotspot? 1, Serial Collector

In serial collector, both the new generation and the old age/permanent generation, are serially executed, using a CPU. When the GC is executed, the application is paused and is not executed.

Young generational's recycling methods:

The surviving objects of the Eden zone are copied to the empty to zone in the survivor. If there are large objects that cannot fit into the area, they are copied directly to the old generational.

The relatively young objects that are alive in the from region are copied to the to region. The surviving relative old object is copied to the older generational.

When the to area is not enough to accommodate the surviving objects, these surviving objects are copied directly to the old generational, no matter how many times they experience young generational collection.

Empty Eden and from, and now from becomes to, and to becomes from.

Before recycling:

after recycling:

Old generational (permanent generational) recovery method

Serial Collector the algorithm of Mark-sweep-compact to the old generational region. The algorithm is divided into three steps, as follows:

mark-marks the object that is still alive.

Sweep -Cleans up garbage objects.

Compact -Move compression is performed to translate all the surviving objects to the start area. Let the tail leave a continuous space, so that the object can quickly allocate space behind.

before and after recycling:

2, parallel collector young generational recovery method:

Just like the serial collector algorithm, it takes advantage of multiple cores to execute in parallel. Blue is a parallel application, and yellow is a GC program.

Old generational (permanent generational) recovery method:

Just like the serial collector algorithm, it takes advantage of multiple cores to execute in parallel.

JVM memory model and Hotspot's GC policy

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.