Java Virtual Machine Series 1: How are Java files loaded and executed
Java Virtual Machine Series 2: Class bytecode detailed analysis
Java Virtual Machine Series 2: Runtime data area parsing
Runtime Data Area: Run-time data-area, hereafter referred to as RDA.
Why do I need to see the JVM Runtime Data area (RDA)?
RDA explains the instruction execution, object allocation, object storage, object reading, object release and other key issues, familiar with RDA can help us analyze:
- Under what circumstances will Gc occur, and how to do if frequent Gc causes stutter?
- How is OOM/StackOverflow created?
- Does an instruction counter overflow occur?
- What’s wrong with concurrency, what is instruction reordering, and what does volatile do?
These questions will not all be answered in this article, but it is true that those who do not understand RDA are just like seeing flowers in a fog.
Ii.RDA composition structure
RDA is to facilitate the storage of information needed by the runtime, convenient for CPU to find the corresponding content when executing instructions, such as class information, object information, the current running instruction line number, some constant information and so on.
RDA consists of the following five parts
- PC register
- The virtual machine stack
- Local method stack
- The heap
- Methods area
Among them, the virtual machine stack and method area are relatively complex
1. PC registers
- Thread-private
- For Java methods, it points to the address of the instruction that the method is currently executing
- It is the same size as the bit width of the system, so it can hold any address, so there is no OOM CPU bits = the number of registers in the CPU = the width of data that the CPU can process in parallel at one time
2. Vm stack
- Thread private
- Is a lifO stack structure
- The context in which a stored content method executes is called a stack frame, as analyzed below.
- During method execution, a StackOverflowError is raised when the number of stack frames opened exceeds the limit
- An OutofMemoryError is thrown if the stack can automatically expand the space and no more memory can be allocated while expanding the space
3. Local method stack
- Thread private
- When calling public Final Native XXXX from Java, the called native method (usually C/C ++) will enter the local method stack
- If the local method stack needs to call a Java method, a new Java stack frame is created and pushed into the virtual machine stack
- The exception handling mechanism is consistent with that of VM frames
4. The heap
- Threads share
- Heap space should not be contiguous memory space
- It holds object and array information
- It does not need to display the collection, and the collection is handled by THE GC. The collection algorithm will be discussed in the next section
- OutOfMemoryError is raised when more memory cannot be allocated
- The newly created objects are allocated in the new generation, and the objects that survive after GC are allocated in the old generation. If the objects are too large, the memory of the new generation is not enough, they may be allocated in the old generation directly.
5. Methods area
- Interthread sharing
- An outOfMemoryError occurs when the method area cannot meet memory allocation requirements
- Before JDK 7, the method area was in the permanent generation of the heap, and jdK8 was put into the meta space and did not occupy the vm memory size
- Stores loaded type information, constants, static variables, and just-in-time compiler-compiled code caches that are essentially loaded into the method area after parsing the class bytecode data
Specifically, the contents of the method area include
Type information (class_info)
The types include: the class/interface/enum/annotation for these types, in the methods section to save the following contents:
- The full type name, which is the package name. The name of the class
- The type of access qualifier private/public/protected/static/final
- The full type name of the immediate parent
Properties/Domain information (field_info)
- The attribute name
- Property access qualifier
- Attribute type information
Method information (method_info)
- Method names
- Method access qualifier
- Method return type
- Method parameter type
- Exception table The exception table marks the start and end ranges of instructions for exception handling, as well as the instruction locations where the PC register should jump if an exception occurs
Runtime Constant pool (Constant_pool)
The runtime constant pool contains constant information defined in constant_pool. Constant information in class files is called a static constant pool, and when loaded into the virtual machine method area is called a runtime constant pool. The runtime constant pool also stores constants generated during runtime.
3. The stack frame
Stack frames are the contents stored in the virtual machine stack in the method area, through which the virtual machine manages the execution of the method.
1. Definition of stack frame
A frame is used to store data and partial results, as well as to perform dynamic linking, return values for methods, and dispatch exceptions.
By definition, stack frames are used to hold local variable tables, intermediate run results, dynamic linking, call return data, and handle exception information.
Local variable scale
A list of local variables used to hold our local variables (variables in methods). First of all, it is a 4-byte length, mainly store our Java eight basic data types, if it is 64-bit, use high and low bits to occupy two can also be stored, if it is some local objects, only need to store a reference address of it
In this example, we create 6 local variables a,b,temp,f,g,e. Since g and e are not used, the compiler does not place them in the local variable table, and the first position of all local variables in the table is this reference.
The operand stack
The first-in-last-out stack structure that holds operands executed by Java methods. The elements of an operation can be any Java data type, and the operand stack of a method is empty when it starts. At run time, intermediate results are placed in the operand stack.
Dynamic link
The Java language is polymorphic. Generally defined methods, such as static or private methods, are not overloaded, so the reference index of a method can be replaced directly with the method’s address during compilation, a process called static binding.
If the method cannot be determined by the compiler, it is still necessary to keep the index of the method and retrieve the address of the real method in the runtime constant area according to the call.
Method exit address
After the method call, the contents of the PC register are changed to the value of the method exit address + 1 according to the method exit address, so that when the current method frame is off the stack, the PC register can find the next instruction to continue execution.
Exception handling
Each method has an exception table that records the scope of the exception table and the address to jump to when a try/catch logic occurs in the method
The figure contains a try/catch method, which generates the Exception table jump at the 6th PC instruction, corresponding to the source code at line 5, which is the catch(Exception e) location.
2. The working principle of stack frame
When a thread is running, it will pack each method into a stack frame, which stores the local variable table, operand stack, dynamic link, method exit and other information, and then put it on the stack. The current method being executed at each moment is the frame at the top of the virtual machine stack. The execution of the method corresponds to the process of pushing and removing frames from the virtual machine stack
As shown in the figure, method 1 calls method 2, and method 2 calls method 3. There are three stack frames in the corresponding VIRTUAL machine stack. The top element of stack is the stack frame of method 3.