Jvm memory analysis and GC personal learning summary, jvmgc

Source: Internet
Author: User

Jvm memory analysis and GC personal learning summary, jvmgc

Since its birth, Java is still the most widely used language over the past 20 years. This relies on the various technologies and features provided by Java, allowing developers to write efficient programs elegantly. Today we will talk about a basic but very important Java technology.Memory Management

Everyone who knows C language knows that the development and release of memory in C language are managed by ourselves. Every new operation requires a delete operation, otherwise, the parameter Memory leakage and overflow will occur, resulting in very poor results. However, you do not need to worry about this issue during Java development. Because jvm provides an automatic memory management mechanism. The jvm helps us with memory management. In this way, we don't have to worry about releasing the memory.

Jvm memory Analysis

Although the jvm has helped us with memory management, we still need to know what the jvm has done. Let's take a look at it.

Jvm starts a series of tasks, one of which is to open up a piece of runtime memory. This memory is divided into five areas for different functions.

 

 

Program counters

Record the address of the next command for running the program. The "Address" here can be a local pointer or the offset between the method bytecode and the start instruction of the method. If the thread is executing a local method, the program counter value is "undefined ". in a multi-threaded environment, each thread has its own program counter. During jvm scheduling thread, the program counter of the current thread is saved to the snapshot, so that the next time the thread gets the execution time

VM Stack

A Virtual Machine stack is a memory model for Java method execution. When each method is executed, a frame is created in the stack to store the local variable table, operand stack, dynamic link, and method exit. When the method starts to be called, stack frames will be created and merged into the stack, and the stack will be generated at the end of the method execution. Each thread has its own stack.

Dynamic Link:

Method Exit:

The stack size can be configured through-xxs. When nested calling is improperly used, the method will not stop going into the stack, resulting in StackOverflowError because the stack space is full.

Local method Stack

Heap

A heap is used to store object instances. Almost all object instances are allocated in the heap. The heap is shared by threads, which is the reason for the synchronization mechanism in multi-thread mode.

Heap is the main area of GC management. Before GC recycles the heap, first determine whether the object is dead (the object cannot be used again)

There are two algorithms for determining whether an object is alive: The reference counting algorithm and the Accessibility analysis algorithm.

The reference counting algorithm adds a reference counter for each object. Every time a reference points to it, the counter is incremented. Objects whose counter is 0 at any time cannot be used. This algorithm is easy to implement, but it is difficult to solve the problem of object circular references (for details about circular references, see the remarks below)

The Accessibility analysis algorithm is an algorithm currently in use in Java. Its basic idea is to use a system called "GC Root" as the starting point to search down from this starting point. The paths searched through are called reference chains, when an object is no longer referenced in a chain, it indicates that this object cannot be used again.

In Java, GC Root includes the following objects:

It can be seen that the existence of the analysis object is related to reference. After JDK1.2, Java expanded the concept of Reference, which divided references into Strong Reference, Soft Reference, and Weak Reference) phantom Reference)

A strong reference is a reference in the original sense. As long as a strong reference exists, the referenced object will not be recycled.

SoftReference class indicates soft reference. For objects associated with soft reference, when the system is about to experience memory overflow, these objects will be included in the recycle range for secondary recovery.

The WeakReference class indicates a weak reference. objects associated with a weak reference can only survive until the next garbage collection occurs.

The PhantomReference class indicates a virtual reference. A Virtual Reference does not affect the survival time of the associated object, nor can it obtain an object instance. Its only function is to receive a system notification when the object is recycled by GC.

The heap size can be controlled through-Xmx and-Xms. For mainstream Jvm, GC basically adopts the generational collection algorithm. Based on this algorithm, Java heap is further divided into Young Generation and Old Generation. The new Generation is further divided into Eden and golden vor. Finally, golden VOR is composed of FromSpace and ToSpace. The newly created objects are allocated memory by the new generation. When the Eden space is insufficient, the surviving objects are transferred to the same vor. The new generation size can be controlled by-Xmn, or-XX: limit vorratio to control the ratio of Eden to limit vor. The old generation is used to store objects that have survived many times of garbage collection (I .e., Minor GC) in the new generation.

Permanent Space is the method Area

 

 

Method Area

The method area is shared by threads to store loaded class information, static variables, constants, and Code Compiled by the real-time compiler. -XX: MaxPermSize is used to set the size of the method area.

Direct Memory

Direct Memory is not part of the data zone when the VM is running. The Native function library directly allocates off-heap memory, and then uses the DirectByteBuffer object stored in the Java heap as a reference for this memory.

Memory Allocation and recovery policies

So far, jvmThree mature garbage collection algorithms have been developed: 1.Mark-Purge algorithm; 2.Copy algorithm; 3.Mark-Sort algorithms; 4.Generational collection Algorithm

1.Mark-Purge Algorithm

This type of garbage collection can be divided into two phases: marking and clearing. First, mark all objects to be recycled, and then reclaim all marked objects. This collection algorithm generates a large number of discontinuous memory fragments. When a large object needs to be frequently allocated, the jvm cannot find a large enough continuous memory block in the new generation, this will cause frequent jvm memory reclaim (currently there is a mechanism for allocating large objects directly to the old age)

2.Copy Algorithm

This algorithm divides the memory into two equal blocks and uses only one of them at a time. When this memory is not enough for use, we will copy the surviving objects to another memory, and then clear the memory once. This method is highly efficient and avoids memory fragments. However, this reduces the amount of memory space that can be used by half.

3.Mark-Sorting Algorithm

This is an upgraded version of the tag-clear algorithm. After the tag phase is completed, instead of directly cleaning the recyclable objects, the surviving objects are moved to one end, and the memory outside the boundary is cleared.

4.Generational collection Algorithm

Currently, commercial virtual machines use this algorithm. First, the memory is divided into several parts based on different object lifecycles, namely, the new generation and the old generation. Then, different collection algorithms are used based on the characteristics of different ages. In the new generation, a large number of objects died in each garbage collection, and only a small number of objects survived. Therefore, the replication algorithm was selected. In the old age, because the object has a high survival rate, the tag-sorting algorithm (or the tag-clearing algorithm) is used)

GCExecution mechanism

Because the object is divided into generations, the garbage collection area and time are different. There are two types of GC: Scavenge GC and Full GC.

  MinorGC

Generally, when a new object is generated and the Eden application fails, Minor is triggered. GC: Perform GC on the Eden region, clear non-surviving objects, and move surviving objects to the same vor region. Then, sort out the two zones in the same vor. In this way, GC is performed on the Eden area of the young generation and will not affect the old generation. Because most objects start from the Eden area and the Eden area is not allocated much, GC in the Eden area is performed frequently. Therefore, it is generally necessary to use fast and efficient algorithms so that Eden can be idle as soon as possible.

  Full GC

Organize the entire heap, including Young, Tenured, and Perm. Full GC, because the whole heap needs to be recycled GC is slow, so we should minimize the number of Full GC times. In the process of JVM optimization, a major part of the work is to adjust FullGC. Full GC may occur due to the following reasons:

1. The Tenured is full.

2. The permanent generation (Perm) is full.

3. The System. gc () is displayed and called.

4. Dynamic Change of Heap allocation policies for each domain after the last GC

JavaCommon Memory leakage

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.