memory allocation: When the JVM is running, it divides the memory space, which is called the runtime data area.
( Note: When a Java source program is compiled into a class bytecode file, the bytecode file is stored in a binary assembly command, and when the program runs, the JVM will interpret the binary command line to the CPU to execute )
The run-time data area is divided into the following pieces:
1) Stack:
Each thread runs with a stack (line stacks), and the data stored in the stack is unique to the current thread. And the stack is stored in the stack frame, when the thread calls a method, it will form a stack frame, and the stack frame is pressed stack operation, when the method executes after the stack frame will be out of the stack operation. This stack frame includes (local variables, operand stacks, Chang references to the corresponding class of the current method, the return address of the method, and so on).
(Note: Because local variables are stored in the stack, and each thread corresponds to its own line stacks, local variables are thread-safe and do not result in resource sharing.) )
2) Local method stack:
The mechanism of the local method stack is similar to the mechanism of the stack, except that the stack is the method implemented by Java, and the local method stack is the local method that runs (Native). The so-called local method refers to methods that are implemented in the local JVM that require calls to non-Java languages, such as the C language. In the JVM specification, in fact, there is no mandatory requirement that the implementation party must divide the local method stack and the concrete implementation, this part can be implemented according to the implementation of the specific requirements. Therefore, in the implementation of Hotsport virtual machine, the method stack and the local method stack are combined into one.
3) Program counter:
Program counters can also be called PC registers. It is primarily used to store the next instruction address to be executed by the current program. The CPU will find the corresponding instruction according to this address to execute. Popular speaking is the instruction cache. This register is implemented internally by the JVM and is not a physical concept register, but the JVM is logically the same as the implementation of the function
4) Heap:
The heap memory primarily holds the objects created and arrays. Heap memory is an area that can be shared by multiple threads, so multiple line stacks can access the memory area of the same heap. Each object in the heap holds an instance variable for that instance. When a local variable is defined in the method, if the variable is a basic data type, the value of the variable is stored directly in the stack, and if the variable is a reference data type, then the object variable is stored in the heap memory, and the stack contains a pointer to the first address of the object in the heap memory.
(Note: All types except 8 base data types in Java are reference data types)
JVM Memory allocation