Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the memory allocation and recovery mechanism of Java?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the memory allocation and recovery mechanism of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

one。 Runtime data region

The following figure is a schematic diagram of memory when the Java virtual machine is running:

From the figure, we can see that Java memory is divided into six parts:

Program counter: each thread has a separate program counter, which can be seen as a line number indicator of the bytecode executed by the current thread. When the bytecode interpreter works, it depends on this counter to select the next bytecode instruction, branch, loop, jump, exception handling, thread recovery and other basic functions.

Java virtual machine stack: the virtual machine stack is thread private and has the same life cycle as the thread. The virtual machine stack describes the memory model for Java method execution, and each method creates a stack frame to store local variables, Operand stack, dynamic link, method exit and other information. From the call to the completion of execution, each method corresponds to a stack frame in the virtual machine stack from the stack to the unstack process.

Local method stack: similar to the role of the virtual machine stack. The difference is that the virtual machine stack serves to execute Java methods, and the local method stack serves Native methods.

Heap: an area shared by all threads. Created when the virtual machine starts, almost all object instances are allocated on the heap. Java heap can also be subdivided into new generation and old age, and in more detail, there are Eden space, From Survivor space and To Survivor space. However, no matter how partitioned, the object instances are stored, and the purpose of further partitioning is to better reclaim memory or allocate memory faster.

Method area: the method area is the memory area shared by each thread, which is mainly used to store class information, constants, static variables, immediately compiled code and other data that have been loaded by the virtual machine. This area, like the Java heap, does not require contiguous memory and can choose not to implement garbage collection except for fixed size or expandability. The goal of memory collection in this area is mainly for constant pool recycling and type unloading, and garbage collection behavior is rare in this area.

Run-time pool: the run-time pool is part of the method area. In addition to the version, field, method, interface and other description information of the class, another piece of information in the Class file is the constant pool, which is used to store various literals and symbol references generated during compilation. This part of the content is stored in the runtime pool of the method area after the class is loaded.

Direct memory: direct memory, also known as out-of-heap memory, is not part of the virtual machine runtime data area. After JDK1.4, NIO class is introduced, which is based on channel (Channel) and buffer (Buffer). It can use Native function library to allocate memory directly outside the heap, and then manipulate this memory through DirectByteBuffer objects stored in the Java heap as references. This significantly improves performance and avoids copying data back and forth between the Java heap and the Native heap.

So it is summarized in the form of a table as follows:

Data area summary thread sharing program counter line number indicator of bytecode executed by current thread No virtual machine stack creates a stack for Java method execution to store local variables, Operand stack, dynamic link, method exit, etc. No local method stack is similar to virtual machine stack For Native method service no heap to store object instances is the method area to store the class information, constants, static variables, immediately compiled code and other data loaded by the virtual machine is a part of the runtime constant pool method area. The literals and symbolic references generated during the compilation period are directly allocated to the memory outside the heap, which has high performance and is not limited by the size of the Java heap. Object creation and memory layout

1. Creation of object

Creation of Java object

The figure above is a complete flowchart of object creation, which is explained in more detail next.

When the virtual machine receives the new instruction, check whether the parameters of the instruction can locate the symbolic reference of a class in the constant pool, and check whether the class represented by the symbolic reference has been loaded, parsed and initialized. If not, you must first perform the class loading process.

You can determine the space required for object allocation after the class is loaded. If the memory in the Java heap is absolutely regular, the used memory side is stored on one side, and the idle memory side is stored on the other side, with a pointer in the middle as an indicator of the demarcation point, then the allocation of memory only moves the pointer to the free space direction for a distance equal to the size of the object, which is called "pointer collision". If the memory in the Java heap is not regular and the free memory and used memory are interlaced, the virtual machine must maintain a list, record which memory blocks are available, find enough space from the list to allocate to the object instance, and update the records on the list, which is called the "free list". Which allocation method is used is usually determined by whether the garbage collector of the virtual machine has the function of compression and collation.

When dividing available space, you also need to consider whether it is thread-safe to allocate space for object instances. There are two ways to ensure thread safety. One is to synchronize the actions of allocating memory space. In fact, the virtual machine uses CAS with failed retry to ensure the atomicity of the update operation. The other is to divide the memory allocation into different spaces according to threads, and each thread pre-allocates a small piece of memory in the Java heap, which is called local thread allocation buffer (Thread Local Allocation Buffer, TLAB). Whichever thread allocates memory, it is allocated on the TLAB of the thread, and synchronous locking is required only when the TLAB runs out and allocates a new TLAB.

After the memory allocation is completed, the virtual machine initializes the allocated memory space to zero (excluding the object header) to ensure that the instance field of the object can be used directly without the initial value in the Java code.

The virtual machine puts the information of the object into the object header of the object.

Execute constructor

two。 Memory layout of the object

The memory layout of the object is divided into three parts:

There are two main pieces of information in the object header:

Part of it is used to store the runtime data of the object itself, such as hash code, GC generation age, lock status flag, lock held by thread, biased thread ID, biased timestamp and so on.

The other part is the type pointer, that is, the pointer of the object to its class metadata, which is used by the virtual machine to determine which class the object is. If the object is an Java array, there must also be a piece of data in the object header that records the array length.

The instance data part is not only the valid information really stored by the object, but also the content of various types of fields defined in the program code. What is inherited from the parent class and what is defined in the subclass needs to be recorded.

Alignment padding only acts as a placeholder. HotSpot VM's automatic memory management system requires that the starting address of an object is an integral multiple of 8 bytes, so the object size must be an integral multiple of 8 bytes. When the data part of the object instance is not aligned, it needs to be filled by alignment.

Here I would like to recommend a Java advanced group: 725633148 will share some videos recorded by senior architects: (there are Spring,MyBatis,Netty source code analysis, principles of high concurrency, high performance, distributed, micro-service architecture, JVM performance optimization, distributed architecture, interview materials), etc., which have become essential knowledge systems for architects to get free of charge.

three。 Memory recovery

1. Determination of object survival

The Java virtual machine determines whether the object is alive or not through reachability analysis. The basic idea of this algorithm is to search down from these nodes through a series of objects called "GC Roots". The path of search is called reference chain. When an object to GC Roots is not connected to any reference chain, the object is not available.

As shown in the figure, object5,object6,object7 is related to each other, but GC Roots is unreachable, so they are determined to be recyclable objects.

It is also worth mentioning the reference counting algorithm, which gives the object a reference counter, which increases the counter value whenever a place references it; when the reference expires, the counter value is reduced by one; an object with a counter of 0 at any time can no longer be used. The reference counter is efficient and easy to implement. However, it is difficult to solve the problem of circular references between objects, and most mainstream Java virtual machines no longer use reference counting to manage memory.

Schematic diagram of accessibility analysis

Even objects that are unreachable in the reachability analysis algorithm are not necessarily immediately recycled. An object is recycled and goes through at least two tagging processes.

If an object does not have a chain of references connected to the GC Roots after reachability analysis, it will be marked for the first time and filtered. The filter condition is whether it is necessary for this object to execute the finalize () method. When the object does not override the finalize () method, or when the finalize () method has been called by the virtual machine, the virtual machine treats both cases as "unnecessary."

If the object determines that it is necessary to execute the finalize () method, the object is placed in the F-Queue queue, which is later automatically created by the virtual machine, and the low-priority Finalizer thread executes the finalize () method. GC makes a second small mark on the object in F-Queue, and if the object is re-associated with any object in the reference chain, it will be removed from the collection "about to be recycled" the second time. Otherwise, the object will really be recycled.

Finalize method

two。 Method area recovery decision

The recycling of the method area mainly includes two parts: abandoned constants and useless classes.

Recycling of obsolete constants is similar to recycling objects in the Java heap.

The conditions for judging useless classes must meet three conditions:

All instances of this class have been recycled.

The ClassLoader that loaded the class has been recycled.

The corresponding java.lang.Class object of this class is not referenced anywhere and cannot be accessed through reflection.

3. Garbage collection algorithm

Tag-clear algorithm (Mark-Sweep):

The algorithm is divided into two stages: "marking" and "clearing": first, mark the objects that need to be recycled, and then reclaim the marked objects uniformly after the marking is completed. It has two main shortcomings: one is the problem of efficiency, the two processes of labeling and removal are not efficient. The second is the space problem, a large number of discontiguous memory fragments will be generated after mark removal, and too many fragments may cause a garbage collection action to be triggered in advance when you cannot find enough memory space for larger objects.

Mark-clear

Replication algorithm:

The replication algorithm divides the available memory into two equal chunks of capacity, using only one of them at a time. When one piece of memory is used up, copy the surviving objects to another, and then clean up the used memory space at once. In this way, the memory of the whole half area is reclaimed every time, and the memory fragments are not considered when the memory is allocated, as long as the pointer at the top of the stack is moved and the memory is allocated sequentially, which is simple and efficient. It's just that this algorithm reduces the memory to half of the original, and the cost is high.

Replication algorithm

Tag-collation algorithm (Mark-Compact):

The marking process is the same as the "mark-clear" algorithm, but the subsequent process is not to clean up the recyclable objects directly, but to move all the surviving objects to one end, and then directly clean up the memory outside the end boundary.

Marking-finishing algorithm

4. Generation collection algorithm

The garbage collection of commercial virtual machines uses a generation-by-generation collection algorithm to divide the memory into several blocks according to the object survival cycle. Java heap is divided into new generation and old age, so that the appropriate collection algorithm can be adopted according to the characteristics of chronology. In the new generation, a large number of objects die in each garbage collection, so choose the replication algorithm. The survival rate of old-age objects is high, and there is no extra space for allocation guarantee, so it is suitable to use "mark-clean" or "mark-organize" algorithm to recycle.

4. Memory allocation and recovery strategy

Object takes precedence in the Eden partition:

In most cases, objects are allocated in the Cenozoic Eden zone. When there is not enough space allocated to the Eden zone, the virtual machine initiates a Minor GC. After GC, the object tries to put into the Survivor space. If the Survivor space cannot be put into the object, it can only be transferred to the old age in advance through the space allocation guarantee mechanism.

The big object goes straight into the old age:

Large objects are Java objects that require a large amount of contiguous memory space. The virtual machine provides the-XX:PretenureSizeThreshold parameter, which is allocated directly in the old age if it is greater than this set value object. In this way, a large amount of memory replication can be avoided in the Eden area and two Survivor regions in the new generation.

Long-term surviving objects enter the old age:

The virtual machine defines an object age counter for each object. If the object is born in Eden and survives after a Minor GC, and can be accommodated by Survivor, it will be moved to Survivor space and the age of the object will be set to 1. After each Minor GC, the object still lives in the Survivor area, the age is increased by one, and when the age reaches the value set by the-XX:MaxTenuringThreshold parameter, it will move to the old age.

Dynamic age judgment:

Virtual machines do not always require that the age of the object must reach the value set by-XX:MaxTenuringThreshold in order to move the object to the old age. If the total size of all objects of the same age in Survivor is more than half of the Survivor space, objects older than or equal to that age can enter the old age directly.

Space allocation guarantee:

Before Minor GC, the virtual machine checks whether the maximum available contiguous space in the old era is greater than the total space of all objects in the new generation, and if the condition is true, then Minor GC is true. If not, the virtual machine checks to see if the HandlePromotionFailure setting allows the guarantee to fail. If allowed, it will continue to check whether the maximum available contiguous space in the old age is larger than the average size of the object moved to the old age, and if so, an Minor GC will be tried. If it is less than, or if the HandlePromotionFailure setting does not allow risk, a Full GC will be done.

New generation GC (Minor GC): garbage collection occurs in the new generation. Because most Java objects die overnight, Minor GC is very frequent and the collection speed is fast.

GC (Major GC/Full GC): garbage collection actions that took place in the old days. The emergence of Major GC is often accompanied by at least one Minor GC. Major GC is generally more than 10 times slower than Minor GC.

This is the end of the content of "what is the memory allocation and recovery mechanism of Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report