The main structure of the JVM is shown in the picture, which refers to the diary of a self-soothing caprice.
Method areas and heaps are shared by all threads, and other areas are thread private
Program counter (Counter Register)
Similar to the PC register, is a small area of memory, through the value of the program counter to find the code to execute the bytecode, because the switch between multiple threads to restore the current execution location of each thread, so each thread has its own program calculator. There will be no outofmemeryerror in this area. When executing a Java method, the address of the executed instruction is stored here, and if the local method is executed, the value here is undefined.
Virtual machine stack (Java stack)
The virtual machine stack is also thread-private, each creation of a thread, the virtual machine will create a virtual machine stack for this thread, the virtual machine stack represents the Java method execution of the memory model, each call to a method, will generate a stack frame for the storage method of the local variable table, the Operation Stack, Method export information, when this method is finished, the corresponding stack frame will be popped.
If the requested stack is too deep, the virtual machine may throw a stackoverflowerror exception, and if the virtual machine's implementation allows the virtual machine stack to scale dynamically, it throws outofmemoryerror when the memory is not enough to expand the stack. exception.
Stack frames (Stack frame)
The stack frame is divided into three parts: the local variable area (locally Variables), the operand stack (Operand stack), and the frame data area.
Local variable area (Loca Variables)
The local variable area is organized into a 0-based word group, where byte, short, and Char are converted to Int,boolean before being stored and converted to int,0 for false, not 0 for True,long and double to occupy two words in length.
operand stack (Operand stack)
The operand stack is also organized into a word group, but unlike a local variable area, it is not accessed by an array subscript, but is able to push and pop operations over the stack, and the previous action push data can be used by the next action pop.
Frame Data area
The role of this part is mainly in three parts:
- Parsing of data in a constant pool
- After the method has finished executing the processing method returns, restores the caller site
- Exception handling when an exception is thrown during the execution of a method, the store has an exception table, and when an exception occurs, the virtual machine looks for the corresponding exception table to see if there is a corresponding catch statement, and if no exception is thrown, the method call is terminated.
Local methods Stack (Native method Stacks)
Similar to a virtual machine stack, only used when performing local methods.
Method area
Used to store information such as type information, constants, static variables, immediately compiled code, and so on, that have been loaded by the virtual machine.
The method area is shared between threads, and when two threads need to load one type at the same time, only one class requests ClassLoader loading and the other thread waits.
For each loaded type, the following information is saved in the method area:
- The fully qualified name of the class and its parent class (Java.lang.Object has no parent)
- Type of Class (class or Interface)
- Access modifiers (public, abstract, final)
- A list of fully qualified names of the implemented interfaces
- Constant pool
- Field information
- Method information
- Static variables in addition to constants
- ClassLoader references
- Class reference
For each field, the following information is saved in the method area (the field declaration order is also saved):
- Field name
- Type of field
- Modifier for field (public, private, protected, static, final, volatile, transient)
For each method, the following information is saved in the method area (the method declaration order is also saved):
- Method name
- Method return type (or void)
- Parameter information
- Method modifiers (public, private, protected, static, final, synchronized, native, abstract)
If the method is not an abstract method and is not a local method (Native), the following information is also saved:
- Byte code of the method
- The size of the local variable table and the operand stack
- Exception table
A virtual machine needs to store some data to quickly access a method in a class object, typically implemented as a method table.
Another part of the method area is the run-time-constant pool, which is used primarily to store literal and symbolic references generated during compilation, and constants can be generated at run time, such as the Intern method of string.
GC may also exist in the method area, but the virtual machine specification does not require it, mainly to reclaim some constants and unload some of the unused type information, but the condition of unloading a class is difficult to achieve, and in some cases the GC does not actually reclaim much memory.
Heaps (heap)
Where a virtual machine is used to hold objects and array instances, the main area of garbage collection is here (and possibly the method area).
If garbage collection algorithms are collected on a per-generation basis (most of the time), this section can be subdivided into the new generation and the old age.
The Cenozoic may also be classified as Eden, from survivor and to survivor, mainly for garbage collection. All threads share the Java heap, where you can also divide the thread-private buffers (thread Local Allocation buffer,tlab).
The Java heap is only required to be logically contiguous and can be discontinuous on the physical space.
Direct Memory
NiO is referenced in JDK1.4 and the channel and buffer are referenced, and the native library can be used to directly allocate out-of-heap memory and operate through a Directbytebuffer object stored in the Java heap as a reference to this memory.
Object access
When a new object is created, it allocates memory for the object in the heap and has a reference to the object in the stack, in addition to this object in the Java heap to find its type information (object type, parent class, implemented interface, including fields and methods, etc.).
Reference is defined in a Java virtual machine as a reference to an object, but there is no definition of how this reference should be implemented.
An implementation is a reference directly stores an object's address within the heap, and the type information of the object can be stored in the memory layout of the object in the heap, such as at the beginning of the object's memory.
Another implementation is that reference points to a position in a handle table that holds the actual location of the object and the type information it corresponds to.
The advantage of using a handle is that when you move the object's position in memory, you only need to update the contents of the handle table, without changing the reference value, but with more memory access overhead, the pros and cons of the direct reference are the opposite.
Virtual machine Memory Structure