The Java Virtual machine performs a Java program by dividing the area of memory that it manages into several different data regions. Each of these areas has its own purpose, and some areas exist as the virtual machine process starts, and some areas are built and destroyed depending on the user thread's start and end. The memory managed by the Java Virtual machine will include the following runtime data regions.
Program counter
Program counter is a small amount of memory space, its role can be seen as the current thread execution of the byte code indicator, the bytecode interpreter works by changing the value of this counter to select the next need to execute the bytecode instruction, branch, loop, jump, exception handling, Basic functions such as thread recovery need to rely on this counter to complete.
Because each thread has a separate program counter to ensure that the virtual machine can be switched back to the correct execution location (using thread-private memory, each thread does not affect each other).
If the thread is currently executing Java code, this counter is logged with the address of the virtual machine bytecode instruction being executed, and if it is the native method, the counter value is empty. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.
Java Virtual Machine stack
Like a program counter, it is thread-private, with the same life cycle as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each method is executed to create a stack frame for storing local variable tables, operations stacks, dynamic links, method exits, and so on. Each method is invoked until the completion of the process, which corresponds to a stack frame in the virtual machine stack from the stack to the stack.
The local variable table holds the various basic data types (boolean,byte,char,short,int,float,long,double), object references (reference) that are known during the compilation period, ReturnAddress (point to the address of a bytecode directive).
Data of long and double types consumes 2 local variable spaces (slots), and the other data types occupy only one. The local variable table is required within the programming period to complete the allocation, when entering a method, this method needs to allocate how much local variable space in the frame is fully determined, during the operation of the method will not change the size of the local variable table.
Local method Stack
The local method stack is very similar to the virtual machine stack. The difference is that the virtual machine executes the Java Code (bytecode) service for the virtual machine, while the local method stack is the native method service used by the virtual machine.
Java heap
The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this memory area is to hold object instances, and almost all object instances and arrays
Allocate the memory here. (But with the development of the JIT compiler and the gradual maturation of escape analysis techniques, stack allocation, scalar substitution, optimization techniques will lead to some subtle changes, all objects are allocated on the heap becomes less absolute)
The Java heap is the primary area of garbage collection management. Java heap can be subdivided into: the new generation and the old age, in the finer point has: Eden Space, from Survivor space, to survivor space and so on. If viewed from a memory allocation perspective, the thread-shared Java heap may divide multiple thread-private allocation buffers (thread Local Allocation buffer, Tlab).
Method Area (permanent generation)
The method area, like the Java heap, is a thread-shared memory area that stores data such as class information, constants, static variables, and instant compiler-compiled code that are loaded by the virtual machine.
Run a constant-rate pool
Running a constant pool is part of the method area. class file In addition to the class version information, fields, methods, interfaces and other descriptive information, there is a constant pool (Constant pool Table), for the compilation period generated by the various literal and symbolic references
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Memory mechanism description (simple and straightforward)