Recently in the review of the Java Foundation, the Java runtime of the entire memory operation mechanism is curious, read a bit in-depth understanding of Java Virtual Machine advanced features and best practices, the entire JVM operation mechanism is still relatively complex. If you have time to get to know it later, briefly describe it at the level of understanding the program.
First, the memory area of Java at runtime
Tell me a little bit about each area.
Program counter: The line number indicator of the byte code executed by the current thread, loop, thread recovery and other basic functions rely on this counter to complete (thread private, each thread will have its own separate counter).
Local method Stack: similar to the Java Virtual machine stack, but the native method is executed.
Method Area (contains a run-time-constant pool): the area that each thread shares, which is used for data such as class information, constants, static variables, and instant compiler-compiled code that have been loaded by the virtual machine.
Java Virtual machine stack: Also thread-private, with the same life cycle as the thread each method executes will create a stack frame (which will be described in detail) structure is the stack structure, advanced and out.
Java heap: The largest chunk of memory managed by a thread-shared Java Virtual machine, where almost all object instances allocate memory and are the main areas of Java garbage collector management.
Run a constant pool: part of the method area mainly holds the class's version, fields, methods, interfaces, and other descriptive information and constants pools. Constant dominant in the class load in the run-time runtime, which holds the compilation period generated by a variety of literal and symbolic references.
Then there is the loading process of the class
The class loading time is usually
1.new when creating an instance
2. Call the static method of the class
3.class.forname ("");
..... (When the class is initialized, his parent class is initialized together)
The loading of a class is divided into three parts 1. Load
The ClassLoader reads the bytecode file into memory and creates a Java.lang.Class object
2. Connect (1) Verify (2) Prepare
Allocates memory for static field of class, sets system default initial value
(3) Analysis
To replace a symbol reference in a constant pool with a direct reference
3. Initialization (mainly for Staticfield initialization)
1 2
1 The last I value of the graph is 3
2 The last I value of the graph is 2
The assignment is based on the order, but the static code block in the 1 diagram can manipulate the I variable, not in the 2 figure, and the static code block assigns only I.