Steps to enter the data area at runtime
The sample code is as follows:
Step description:
1. JVM requests space from memory
The first step of the JVM is to apply for memory space from the operating system through parameter configuration or default configuration parameters, find the specific memory allocation table according to the size of the space, and then assign the start address and end address of the memory segment to the JVM, and then the JVM performs internal distribution.
2. Initialize the runtime data area (allocate memory)
After the JVM obtains the memory space, it will allocate the memory size of the heap, stack and method area according to the configuration parameters
3. Class loading
The details will be discussed in detail in the follow-up content, here is mainly to put the class into the method area, as well as the static variables and constants in the class into the method area.
4. Execute the method and create the object
Start the main thread and execute the main method. At this time, a Teacher object is generated in the heap memory, and the object reference t1 is stored in the stack. The second object will also be generated on the heap, and the reference will continue to be generated on the stack.
Diagram
to sum up
Summary: JVM applies for memory on the operating system, first initializes the runtime data area, then loads the class into the method area, and finally executes the method. The execution and exit process of the method is reflected in the memory as the pushing and popping of the stack frame in the virtual machine stack.
Of course, the memory distribution of the JVM can also be understood from the above figure as if it is doing a jigsaw puzzle. Classify the data and distribute it to different regions.
At the same time, the objects created during the execution of the method are generally stored in the heap, and finally the objects in the heap also need to be cleaned up by garbage collection. ( Why is it generally in the heap, because of the escape analysis optimization, objects can be allocated directly on the stack ).