JVM runs memory allocation and recycling

Source: Internet
Author: User
Tags reserved advantage

This article from the NetEase cloud community

Luzon Sheng

The biggest feature of the Java language compared to the C language is that programmers don't have to worry too much about Java's memory allocation and recycling, because all of this, Java's virtual machines have helped us. The memory management of the JVM greatly reduces the memory management requirements of the developer, and is not prone to memory leaks and overflows in C language. But once a problem is applied to memory, it can also cause the programmer to be difficult to locate. Therefore, it is very important for the Java programmer to know and understand the memory allocation and recycling of the JVM for code writing and application optimization.

1. JVM Memory model

Java's JVM types are very diverse, and different JVMs vary in memory allocation and recycling mechanisms. We're just here to introduce the most popular Jvm,hotspot VMS, which are the most widely used Java virtual machines today. But the JVM is also updated very quickly, there may be some differences between different versions, but overall, the structure is relatively stable.

When it comes to memory management, the first thing we need to know is the data region of the Java runtime, including the allocation of thread-private data and shared data, and so on. According to the description in the Java Virtual machine, the runtime data region is:

As you can see, the data region of the runtime is divided into 5 parts: The method area, the heap, the virtual machine stack, the local method stack, and the program counter. Let's introduce these 5 sections separately.

1. Method area

The data stored in the method area is shared by each thread, storing data such as the class information loaded by the virtual machine, solid, static variables, compiled code, and so on.

2. Heap

All the data stored in the heap area is shared by each thread, and is the area of memory that we are most concerned about in our program. The purpose of this area is to store object instances, so almost all of the objects in our program are stored in this area. At the same time, the region is the primary area where the JVM performs memory management and recycling.

3. Virtual Machine Stack

The virtual machine stack is the most important area of memory except for the heap, and the data in the virtual machine stack is thread-private. The virtual machine stack is the memory model that the Java method executes, creating a stack frame for storing local variable tables, operand stacks, and so on when each method executes.

4. Local Method Stack

It is very similar to the virtual machine stack, and the difference is that the virtual machine stack is the data structure used to execute the Java method, and the local method stack is the native method service used by Java.

5. Program counter

Program counter is a very small piece of memory, each thread has a separate program counter, through the program counter, we can know the current thread execution of the bytecode ordinal.

The basic functions of the data region and each region of the JVM runtime are briefly understood, and in actual use, we are most concerned with the local variable tables in the heap area and the virtual machine stack. For a thread-private virtual machine stack, data memory is recycled as the thread dies, and the collection of heap data becomes the focus of the JVM's memory management and recycling.

2. The generational mechanism of memory management

In the JVM, the current use of the internal management is a generational way, that is, the memory into the Cenozoic, Laosheng generation and permanent generation. Here we talk about the generational management mechanism is the memory area for thread sharing, mainly the heap, also including the method area.

The advantage of the Java generational mechanism is that it can be created and destroyed according to the actual object of Java, and different garbage collection strategies can be used in different generations to improve the efficiency of garbage collection. In Java, almost all instances of objects are allocated to the new generation, while most objects are not long-lived, and objects in the Cenozoic are recycled more frequently. The older ones are those that survive longer, or are too large to be allocated in the Cenozoic. And the permanent generation is more special, it refers to the memory area of the method area, the hotspot in the implementation of the method area as a permanent generation to deal with, to avoid extra to manage the method area. Memory recycling In this area we generally do not consider, because the effect is not very obvious, and the conditions of recycling is very harsh.

3. Garbage removal algorithm

For different generations and their characteristics, the garbage collection strategy used by different generations is not the same.

3.1 Mark-Clear algorithm

The tag cleanup algorithm is actually very simple, it is to mark those who have died, and then clean up the dead objects. But one big disadvantage of it is that direct cleanup can produce a lot of memory fragmentation, causing the subsequent allocation of memory to trigger the next garbage collection because of fragmentation problems without contiguous large spaces to satisfy allocations. It can be imagined that the garbage removal strategy is not optimal in efficiency and space.

3.2 Copy algorithm

The copy algorithm is essentially no different from the tag-purge algorithm, but it solves the problem of memory fragmentation and solves the problem of two scans. It is implemented by reserving a portion of memory in memory allocation, when the memory needs to be recycled, it will be scanned, the memory data is not expired copied to the reserved memory, and directly clean up the original allocated memory, the original allocated memory as reserved memory. The advantage of this method is that the efficiency is very high, the disadvantage is very obvious, that is to waste a portion of memory as reserved memory, and if in order to ensure that data 100% is not lost, in principle we need to reserve all the allocated memory half, resulting in a large area of memory waste.

In the new generation, the JVM uses the replication algorithm, because the new generation of objects are almost dead, so each garbage collection effect will be more obvious, we also call MINORGC. The Cenozoic is divided into 3 areas, the Eden Zone, the from survivor and to survivor areas. Two survivor are backed up, and when garbage is collected, the objects are centrally copied to the free survivor zone. In order to improve memory utilization, this Eden area occupies a large proportion, the default scale is 8:1. In this way, only 10% of the new generation of memory is wasted, but after all, there is a large number of objects can not be recycled resulting in survivor area space shortage problem. This involves the allocation of security issues, when the survivor area is not enough, the object will go directly into the old age.

3.3 Labeling-Sorting algorithm

In addition to the wasted space, the problem is that if the object is long-lived, it will reduce the efficiency of memory recovery because the replicated memory will become larger. Therefore, the replication algorithm is more suitable for those memory areas where the object is short of survival time recovery. Therefore, based on the replication and labeling-clearing algorithm, a marker-collation algorithm is proposed. The tag-finishing algorithm also marks the object first, and then the algorithm moves the surviving object in one Direction in the memory, and the final memory will be a clear demarcation between the memory occupied and the free memory, which mainly solves the problem of memory fragmentation.

Compared with the new generation of Asao, the object survival time of Laosheng generation will be longer, so the labeling-sorting algorithm is adopted. If a laosheng generation of garbage collection occurs, we call it FULLGC. Laosheng generation of low recovery efficiency, will cause the system to pause for a long time, so we have to minimize the occurrence of FULLGC.

4. Assigning a recycling policy

Above we see the garbage collection algorithm of the JVM generation, let's look at some of the most common points in memory allocation and recycling of the JVM.

4.1 Object optimization allocated in Eden area

Java objects are assigned precedence in the Eden area, and the JVM performs a minorgc once when there is not enough memory allocation in the Eden area. Therefore, the MINORGC in the JVM will be more frequent garbage collection action, the general recovery speed is relatively fast. Object assignment is also not absolute in the Eden area, with one exception being that large objects go directly into the old age. The large objects here refer to Java objects that require contiguous memory space, such as long strings and arrays. Large objects go directly into the old age is very unsuitable for garbage collection strategy, especially these large objects are also those who die in the evening, which will cause more frequent fullgc, resulting in reduced system performance.

4.2 Long-lived objects of the old age

Each object has an object age, and each time the object is garbage collected in the Cenozoic, the object ages by 1, and when the object is older than a certain threshold, the object enters the old age. So here's the problem, if we're doing garbage collection very often, the object's object age will grow fast, and an object will be very easy to do in the old age, causing the FULLGC to grow.

5. The judgment algorithm of object death

We have introduced garbage collection, but have not introduced the algorithm used in the JVM to determine the death of the object. The simplest algorithm for object judgment is to use the counting method. When the object is referenced, the count is added to 1, when the reference count of an object is 0 o'clock, indicating that the object is dead and can be recycled. The counting method is simple, easy to implement, but does not solve the problem of mutual reference, such as Object a reference b,b also reference A, and A and B is no longer referenced by other objects, in this case, if the AB object can be recycled, but the count is not 0.

At present, the universal method of judging the death of the object is the Accessibility analysis algorithm. Accessibility analysis refers to starting from the beginning of an object, if the object can be referenced to, the object is alive, otherwise, the object is dead. So what is the starting point for the most basic object in the algorithm? These objects refer to objects referenced in the virtual machine stack, objects referenced in the method area, and objects referenced in the local method stack.

This article from the NetEase cloud community, by the author Lu Zongxie authorized release

Related article:
"recommended"  obs source code compilation development

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.