Introduction to the
This content
- Relationship between process memory and Java runtime data areas
- Java Memory Model (runtime data area)
- Analog program execution
- Simulate the object creation process
- The JVM heap
Process memory
process
Let’s talk briefly about the process.
Process is a running activity of a program on a data set in a computer. It is the basic unit of resource allocation and scheduling in the system
The software installed on our computer, such as QQ, wechat and 360, runs in the operating system one by one, and the operating system will allocate the memory of the computer to each process.
Process memory
We put the magnifying glass on the memory of the process, in fact, each process internal memory is divided into several areas, roughly divided into code segment, stack segment, heap segment
Java process
Our Java program is also a process. This process loads, parses, stores, and processes class files in memory. The location of the storage is in the process heap
The focus of this article is on what the runtime data area looks like
Runtime data area
The runtime data is divided into method area and virtual machine stack. Local method stack. The heap. Program counter. The following are introduced in turn.
Methods area
Scope: The method area is shared by all threads
Function: The method area is used to store loaded class information, constants, variables and other data
Permanent generation and meta-space
The method area is the “specification (understood as an interface in a program)” given by the Java Virtual Machine specification, and the permanent generation and meta-space are the implementation of the method area at different times (after 1.8 is the meta-space, before the permanent generation).
Klass is a mapping of Java classes in c++. Klass objects in c++ are actually stored in the method area (meta space).
The virtual machine stack
The virtual stack is an area that is private to each thread
Look at this picture and ask yourself a question: How many virtual stacks are there? The obvious answer is as many threads as there are threads.
structure
Take another look inside the virtual machine stack
It’s very simple. It’s a stack. A stack must have a stack frame.
So what’s the stack frame here?
When each method is executed, the Java virtual machine synchronously creates a stack frame
Such as:
public class Test {
public static void main(String[] args) {
add();
}
private static void add(a){}}Copy the code
The corresponding virtual stack is like this.
So let’s analyze this graph.
- After the program runs, the main method is executed first, and a stack frame of the main method is created in the virtual machine stack and pressed. After the add method is called in the main method, a stack frame is created for add and pressed. So main is on top and add is on top.
- How many stack frames are there? After analyzing the first point, it is easy to conclude that there are as many stack frames as there are methods called (note that there are not as many methods as there are methods, since the same method can be called multiple times).
The stack frame structure
Stack frame includes: local variable table, operand stack, dynamic link, return address and other information.
Operand stack and local variable table
If you’re not familiar with bytecode, see the previous article.
public static void main(String[] args) {
int num = 1;
int num2 = 2;
int res = num + num2;
}
Copy the code
opcode
Let’s simulate the execution of this program
- At this point, the content of the scale is locally changed
Remember the order of these variables.
- The constant 1 is pushed onto the operand stack
- The top element pops an assignment to the second variable in the local variable table
- The constant 1 is pushed onto the operand stack
- The top element pops up and assigns a third variable to the local variable table
6. Local variable table first int element pushed
- The second int element of the local variable table is pushed
- Top of the stack add two variables and the result goes to the top of the stack
- The top element pops up and is assigned to the fourth variable in the local variable table
Now that you’ve seen this example, and I’m sure you’re familiar with the operand stack and the local variable table, let’s take a look at what happens when we create an object.
The procedure for the new object
public static void main(String[] args) {
Test test = new Test();
}
Copy the code
opcode
Let’s explain them one by one
- new #2 <com/haozi/Test>
This operator does two things
First: Claim a space on the heap
The second: the operand stack, which pushes the address into this space
Note: at this point, only space is allocated for the object on the heap, and the constructor of the object is not executed, that is, not initialized.
- dup
Copy the top of the stack element and push it to the top
Why make a copy?
Because we need to pop up an element to assign to this, if we don’t make a copy of this, there will be no address on the stack.
- invokespecial
Execute constructor
- astore_1
Assign to the second element of the local variable table
DCL and volatile
Why is the lazy singleton volatile? (Not the point of this article)
Our CPU executes out of order for efficiency, and it is possible to reverse step 3 and step 4 of the previous example, so it is theoretically possible to retrieve uninitialized objects.
The heap
The default size
The minimum is 1/64 of physical memory and the maximum is 1/4 of physical memory
The heap division
The heap is divided into the old age and the New generation (ratio 2:1), and the new sound generation is divided into Eden area, FROM area and to area (ratio 8:1:1).
Why was there so much space in the old days?
Summary: There are more objects to store in the old days.
Details :(don’t understand it doesn’t matter, garbage collection will talk)
The older generation is responsible for storing the following objects
- Objects whose GC is greater than 15 are stored in the 1920s
The object has not been recycled. The object has entered the old age.
- For the new generation of space guarantee
When Cenozoic gc occurs and cannot be released, the object goes directly to old age
Example: the object accounts for 20 megabytes. After YGC, it is not recycled. The FROM area only has 5 megabytes
- Large object. The object is older than half of the Eden area