Home >Java >javaTutorial >JVM memory management------Overview of memory management of JAVA language

JVM memory management------Overview of memory management of JAVA language

黄舟
黄舟Original
2016-12-28 15:26:131259browse

Introduction

Memory management has always been the pride and pride of the JAVA language. It allows JAVA programmers to basically completely ignore the details related to memory management and only focus on business logic. However, no good thing is perfect in the world. While it brings convenience, it also introduces many crazy memory overflow and leak problems.
The terrible thing is not just that. Some programmers who develop in other languages ​​​​put a label on JAVA programmers who "do not understand memory", which is really unacceptable. After all, there are no malloc and delete, no destructors, and no pointers in JAVA. How can programmers who are new to JAVA come into contact with the memory part? What's more, many JAVA programmers are still friends who quit their majors and became monks halfway.
However, although the fact is difficult to accept, there are indeed many JAVA programmers who know nothing about memory. Although mastering the relevant knowledge of memory may not bring about earth-shaking changes and benefits to daily development, but It will still subtly improve your technical level. After understanding memory management, I believe you will have a deep understanding of this.

Memory Division

Talking about the word memory, it is a data storage area that only exists when the program is running. For the division of this area, each virtual machine has its own division method. , but they must comply with the basic specifications of the JAVA virtual machine to implement.
In the virtual machine specification, the memory is divided into six parts, namely PC registers, JAVA virtual machine stack, JAVA heap, method area, runtime constant pool and local method stack.

JAVA Virtual Machine Specification and JAVA Virtual Machine

Here we also need to explain the difference between the JAVA virtual machine specification and the JAVA virtual machine. As the name suggests, the JAVA virtual machine specification is an implementation of the JAVA virtual machine. The specification requirements are formulated by Oracle, and the JAVA virtual machine we usually refer to generally refers to the implementation of a specific JAVA virtual machine specification. For example, the most commonly used JAVA virtual machine hotspot, in fact, there are many implementations of JAVA virtual machine. Even if you have an in-depth understanding of the JAVA virtual machine specification and are interested in it, you can write your own JAVA virtual machine. Of course, the difficulty is not difficult to imagine.

Structural diagram

The following figure is a JVM structure diagram quoted from Baidu Library. Since the runtime constant pool is an area allocated by the method area, there is no runtime constant in this figure. Measurement pool.

JVM memory management------Overview of memory management of JAVA language

Detailed explanation of the memory area

For the above picture, the memory refers to the part of the runtime data area in the rectangular box. Here is a brief introduction to the functions of each part:
1. PC register (thread Unique): The full name is the program counter register, which records the address of the JAVA method currently running by each thread. If a local method is currently executed, the program counter will be an empty address. Its function is to support multi-threading and a series of operations such as thread blocking, recovery, and suspension. Imagine intuitively, how to recover if you do not remember the current running position of each thread. Based on this, each thread has a PC register, which means that the PC register is unique to the thread.
2. JAVA virtual machine stack (unique to threads): The JAVA virtual machine stack is created when a thread is created and is used to store stack frames. The JAVA virtual machine stack is also unique to threads.


Stack frame: Simply put, it can be interpreted as a storage area for temporary data when a method is running. Specifically, it includes data and part of the process results. At the same time, It is also responsible for handling method return values, dynamic linking, and exception dispatching. The stack frame is created when the method is created and destroyed when the method ends. If the method throws an exception, it is also considered the end of the method. However, in each stack frame, there is its own local variable table and operand stack, as well as a reference to the runtime constant pool of the current class.
Local variable table: It is a list of method local variables, which is written into the class file during compilation. For a simple understanding, it can be understood as an object array, which corresponds to each local variable according to the index 0 to length-1. In particular, if it is the local variable table of the instance method, the 0th local variable will be a A reference to the current instance, which is the this keyword, and the rest of the local variables start at index 1.
Operand stack: It is a last-in-first-out (LIFO) stack, and its length is also written into the class file during compilation and is fixed. Its function is to provide space for bytecode instruction operation variable calculation. For example, simply for the sentence int a=9, you need to push 9 into the operand stack first, and then assign 9 to the variable a.

3. JAVA heap (global sharing): This part is the most important part of JAVA memory. The reason why it is the most important part is not because of its importance, but because of the most important part as a developer. part that should be paid attention to. It is created when the JAVA virtual machine is started, stores all object instances and array objects, and has a built-in "automatic memory management system", which is what we often call the garbage collector (GC). The memory release in the JAVA heap is not controlled by the developer and is completely handled by the JAVA virtual machine. The JAVA virtual machine specification does not have clear regulations on how to implement a garbage collector in a JAVA virtual machine. Because of this, the JAVA virtual machine we usually use provides many kinds of garbage collectors, which use different algorithms and implementation methods. Meet various performance needs.
4. Method area (global sharing): The method area is also a component of the heap. It mainly stores the runtime constant pool, field information, method information, the bytecode content of constructors and ordinary functions, and some special method. The biggest difference between it and the JAVA heap is that the information stored is different from the JAVA heap. The biggest difference is that this part of the JAVA virtual machine specification does not mandate the implementation of an automatic memory management system (GC).
5. Local method stack (unique to threads): The local method stack is a traditional stack that is used to support the execution of native methods. If the JAVA virtual machine uses other languages ​​to implement the instruction set interpreter, the native method stack will also be used. If neither of the previous two occurs, that is, if the JAVA virtual machine does not rely on the native method stack, and the JAVA virtual machine does not support native methods, then the native method stack is not needed. If necessary, the local method stack is also created with the startup of each thread.
The above five memory areas, except for the PC register, the other four generally require the JAVA virtual machine implementation to provide customers with size adjustment parameters, which are our commonly used Xms, Xmx, etc.

Memory Management

Memory management is divided into memory allocation and memory release. Take a look at the five memory areas above. In fact, it can be roughly divided into two parts, one is globally shared and the other is thread independent. have.
This part of the memory unique to the thread is created when the thread is started, and when the thread is destroyed, the memory is released. This part of memory does not need to be managed by the garbage collector, but is actively managed by the JAVA virtual machine. Whenever a thread is created, the JAVA virtual machine will allocate the corresponding PC register and JAVA virtual machine stack to it. If necessary If so, there will also be a local method stack. Correspondingly, when a thread is destroyed, the JAVA virtual machine will also release all the memory occupied by this thread.
Compared with the part of memory unique to threads, this part of globally shared memory is more difficult to process, but this is only for the implementation of virtual machines, because this part of memory is required to implement the automatic memory management system (GC) .
Memory allocation for this part of globally shared memory (hereinafter referred to as the heap) is mainly triggered by the programmer's explicit use of the new keyword. As for where and how to allocate this part of new memory, it is decided by the JAVA virtual machine. . The release of this part of memory is managed by the automatic memory management system (hereinafter referred to as GC).
Normally, heap memory allocation depends on the GC strategy and implementation. When allocating, you must consider how to reclaim this part of memory. Because of this, for the explanation of memory allocation, we must first understand how memory is recycled in order to better understand how memory is allocated.

Conclusion

The above is the content of JVM memory management------JAVA language memory management overview. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn