So before we start to introduce the Java memory area, let's put a picture of the memory area so that we can compare it when we introduce it later.
instructions , this article is based on JDK8 to introduce.
Program counter
First of all, it is private to the thread. It is also called the line number indicator of the code. The bytecode interpreter determines the next line of code to be executed by changing the position of the program counter. There is no OOM.
If the thread is executing a Java method, it records the address of the virtual machine bytecode instruction being executed, and if it is a native method, its value is null.
Java virtual machine stack
It is also private to the thread, and its life cycle is the same as that of the thread. When each thread is created, a virtual machine stack is created, and a stack frame is stored inside, and each stack frame corresponds to a method call. Now that you know that the virtual machine stack stores a stack of frames, it is not difficult to guess what is stored in the virtual machine stack.
The Java virtual machine stack has OOM. When the depth of the stack requested by the thread is greater than the depth of the virtual machine stack or the virtual machine stack can be dynamically expanded, when the stack can't apply for enough memory when the stack is expanded, OOM will be thrown.
- internal structure of the virtual machine stack 160bc4f5af0389:
local variable table :
The main storage method parameters, all the basic types of data and object addresses, and the return address type (return address). It uses variable slots as the smallest storage unit. The Java virtual machine does not specify how much memory space a variable slot occupies, but it specifies that a variable slot can store a data type within 32 bits. If the stored data type exceeds 32 bits, such as long, double, then use two variable slots for storage.
operand stack :
The operand stack is a first-in-last-out operand stack. When a method is first executed, a new stack frame will also be created. The operand stack of this method is empty, and it is mainly used to save The intermediate result of the calculation process is also used as a temporary storage space for variables in the calculation process. If the called method has a return value, the return value will be pushed into the operand stack of the current stack frame. The operand stack does not use indexing for data access, but completes data access through push and pop operations.
dynamic link :
The vernacular is that a reference to a method is saved in the stack frame. When the method is executed, you can take this reference to the runtime constant pool to find the method.
The role of dynamic linking is to convert the symbolic references of these methods into direct references to the calling methods.
method returns the address :
That is, after the method execution ends, the value of the next code position to be executed is returned, which is the value of the program counter.
Then in addition to the exit of the method after the normal execution, there is another situation where the method exits caused by an exception, then no value will be returned in this case. For thrown exceptions, no records will be made in the stack frame, but will be recorded in an exception table.
Native method stack
The Java virtual machine stack serves the virtual machine to execute the Java method, and the local method stack serves the local method used by the virtual machine. Like JVM, there are many methods written in C language, and this requires a local method stack to execute.
Java heap
The Java heap is the largest memory space in the virtual machine. It is shared by all threads and is created when the virtual machine starts. Its sole purpose is to store object instances.
If asked in the interview, are all object instances allocated memory in the heap? You must answer at this time, no.
With the development and progress of just-in-time compilation technology, especially the increasingly powerful escape analysis technology, optimization methods such as on-stack allocation and variable replacement have made it no longer absolute to allocate instances in the "heap only".
The Java heap is the main area of garbage collection. The new generation, old generation, permanent generation, etc. often appear in the Java heap. It should be noted here that these are not the physical memory layout of the Java heap. It is divided as a garbage collector. Kind of memory layout.
Method area
The method area is also an area shared by threads. It is mainly used to store data such as type information, constants, static variables, and code cache compiled by the just-in-time compiler that are loaded by the virtual machine.
The method area can be recycled by the garbage collector, mainly for type unloading and constant pool recycling.
The method area can also generate OOM. When the method area cannot meet the new memory allocation requirements, an OutOfMemoryError exception will be thrown.
Runtime constant pool
The runtime constant pool is part of the method area. In addition to the class version, fields, methods, interfaces, and other information in the Class file, there is also a constant pool table, which is used to store various literals and symbol references generated during compilation.
If you don't understand the dynamic link, then look at the running constant pool and then go back to see if it is easy to understand.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。