* This is the 27th day of my participation in the August Gwen Challenge. See the details: August Gwen Challenge
Method area construction changes for jdk1.7 and 1.8 (written now)
Now that you know about class files and the process of class loading, you should also hear some common names such as stack method areas. Let’s look at the JVM memory layout (1.8).
JVM memory foundation (skip those with a little foundation)
The largest classes of JVMS fall into two categories, one shared by threads. One is thread private thread private: JVM virtual machine stack, local method stack, program counter thread shared: heap and method area
Thread private: For example, I wrote a Test other than the main thread thread, they performed at the same time, then we will have two memory thread stack (that is a part of the figure on the right side of the purple to a, all the whole together,) in the figure on the left is the two thread stack, each thread inside there are a number of stack frames, a method corresponding to a stack frame, Thread sharing: for example, if I create an object, the object is stored in the heap, and the variables in the local variable list on the stack only refer to the address of the object, and other threads can also use that address, i.e. the heap is common to all threads
Just to give you a quick overview: the heap is where you put objects like new Test() and the stack is where you put variables like int A in a method and the method area is where you put class information like int A in a global variable of a class and I’m going to talk about each of those sections in more detail
Method area (meta-space)
The method area is the concept of loading class information, and the hotspot VIRTUAL machine’s implementation of the method area is a meta-space (this is the 1.8 implementation, if you need to digitionize more). When a classloader starts up (the classloader is in the heap), it then goes to the host’s hard disk and loads our bytecode files into the JVM method area. The method area now loads information about the Class, references to the Class objects in the heap, and references to the Class loader
That is, the difference between an initialized Class and an uninitialized Class is an additional block of bytecode memory for the method area and an additional Class object for the corresponding Class in the heap, which shows how important the method area is
Static variables and string constants are stored in the method area. This was the case in 1.6, but is now the case in 1.8, depending on JDK versions:
Method area structure changes for jdk1.7 and 1.8
Next, the details of bytecode memory blocks are introduced
- Type information This class is public, right? The final? What is the fully qualified name of the class He fully qualified name of the parent of these is actually saved this class class information
- What are field information modifiers? Private? The public? . What is the type of the field… What is the name of the field… In other words, all information for all fields is stored
- Method information modifier; Method return value; The method name; Local variable table and operand stack size; The bytecode of the method body; The exception table simply holds all the information for all methods
- The constant method name for the run-time constant pool global. Method descriptors, class names, field names, things that don’t change. Because constant pool information is something that can be found quickly, it is accessed by index like an array and is designed for lookup. Keep in mind that constants and variables are two completely different concepts and stored in different ways, as can be seen from the structure above
- A reference to classLoader is shown below
- A reference to Class is shown below
The JVM stack
The virtual machine stack is an in-memory model that describes the execution of Java methods
chart
Local variable table: this is the variable that stores our method at run time
Operand stack: a space used to temporarily store data during operations
Dynamic concatenation: Converting symbolic references to direct references at runtime. Remember, the Class file holds a large number of symbolic references. Some of these symbolic references are converted to direct references during Class loading or the first time they are used. The other part is converted to a direct reference during each run, called the dynamic join.
Return address: It’s very simple. A stack frame represents a method that needs to return to the place where it was called. The return address records this address
It’s abstract. It doesn’t matter. I’ll give you an example
12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Test{ public int count(){ int d = 1; int e = 2; int f = d+e; return f; } public static void main(String[] args) { Test a = new Test(); int k = a.count(); }} |
---|
The first step of the main method execution is that a stack frame representing the main method is pushed, and only the thread is executing
At this point, both a and k have been placed in the local variable table to wait for initialization (the semi-initialized state of the object), and then the object is created (the heap will talk about this process). After the creation, the next value to be assigned to a is evaluated, k: a.mount ().
When a method is called, a stack frame with a count method is pushed onto the stack
Then start executing the count stack frame step above (code execution bytecode from our method area)
1.Create d and e variables and assign default values
2. Start the assignment
D is set to 1
E is assigned 2
We need to evaluate when assigning f, so we push the values of d and e onto the operand stack
And then you add, you pop two values off the operand stack and you add them and then you push them onto the operand stack
And then I’m going to assign 3 to f
The final results
It then returns the value of f and assigns it to k
The count method is removed from the stack, and the main method is continued. The added program counter is used to keep track of the current progress, and can correctly display the number of lines of code executed.
Main is done, it’s out of the stack, it’s done,
So now that you know what a stack is, there’s another thing you need to know about a stack: slot units
Local method stack
The only difference is that a native method stack executes native methods, whereas a JVM stack executes Java methods, which are also thread private
Program counter
It’s a record of where the code is currently executing, but nothing else, right
The heap
The heap is mostly about objects,
The JVM divides the heap into Eden for the young generation, Survive for the old generation, and the proportions shown above
It’s just a partition, and it’s all objects. It’s a partition for garbage collection
Garbage collection is not explained in detail here, but will be covered later in gc
What we’re missing is understanding what an object is, the structure of an object in the heap, which we’ll talk a lot about in the section on object construction, okay
Here are a few nouns that might come up in an interview (or at least have heard of them)
tlab(thread local allocation buffer)
Pointer to the collision
On the stack
Escape analysis