Part of the JVM memory structures (JVM memory model)

What is JVM memory zoning

In fact, this problem is very simple, when the JVM is running the code we write, he must use more than one memory space, different memory space used to put different data, then write the code with our process, to make our system up and running.

To give a simple example, such as we now know JVM is loaded into memory to class for subsequent runs, then I ask you, after these classes are loaded into memory, where he had put it? I thought about this problem?

So the JVM must have an area of ​​memory used to store the classes we wrote.

We define members including class variables, methods, local variables, etc., have a corresponding record is stored in memory to memory jvm.

Methods district

In previous versions of JDK1.8, the representative of an area JVM. After the 1.8 version, the name of this area changed, called "Matespace", can be considered a "meta-data space" This means, of course, this is mainly our own store or write information about various classes.

for example. The following two classes, no class member variables User1, User2 and the class has a class variable of realName.


public class User1 {
	private String userName = "wangwu";
}
public class User2 {
	private static String realName = "zhangsan";
	private String userName = "lisi";
}

复制代码

These two classes are loaded into the JVM, it will be stored (all class variables class will be assigned) method in which this area. As shown below

Program counter (instruction execution codes)

We know that is loaded into the jvm class object is .class file after we write .java files are compiled.

After compiling our code will be compiled into byte code computer can read. And this .calss document is that we compiled byte code is the code.

After loading into memory, the bytecode execution engine began to work. We compiled code instructions to execute out, then the question is, Do we need a memory space to record our current implementation of the bytecode execution engine to which lines of code? This is a special memory area is the "program counter", is used to record the position of bytecode instructions currently executed.

Note: In the case of concurrent execution of multi-threaded environment, the calculator is a certain CPU, the CPU 1 is switched from thread to thread 2, and then switch back to the thread to 1, it is not to know where to step up the implementation of the thread 1, which is the role of the program counter. Thus, when a thread context switch to the previous code again, requires a special record to which the current thread executes one bytecode. Therefore, each thread has its own program counter this.

Stack space, has become a Java Virtual Machine stack (native method stacks do not speak, the contents of the same, just a heap native method of allocating stack space)

When a thread to a method of execution, if this method has local variables, then we need an area to store data local variables. This area is called the java virtual machine stack.

Each thread has its own stack java virtual machine, for example, when the main method of execution will have a main thread, the main method used to store local variables defined in

public class TestController {

	public static void main(String[] args) {
	    int i = 1;
    	    User1 user1=new User1();
    	    user1.setUserName("sss");
	}
}
复制代码

Such as the above main () method, in fact, there is a "user1" local variables, he is a reference instance of an object of a User1, there is a "i" of the local variables in the following figure:

Since it is a stack, it would follow the principle of LIFO. When the method is finished, this will be a stack frame stack, local variables inside information will be deleted from the memory. So local variables are thread-safe. Because only the current thread can get to this value.

Q: Why use LIFO data structure?

A: Suppose b is called a process method, a stack frame at this time a method to stack, the stack frame of the stack method b. When method b is finished, the first stack frame of the stack method b, continue to a method, a stack frame and then process the stack. Therefore, the use of a LIFO stack structure is perfect.

Heap space

Or code above, when the main thread executing main () method, the heap memory in the first instantiated objects User1, then user1 created in a local variable, the memory address is stored user1 instantiated object User1. Then perform the Student object getName () method.

As shown below:

Heap external memory

In fact, many of the underlying code API JDK, such as NIO.

If you look at the source code you will find many parts of the code is not written in java, but the native method call some method to go to the local operating system inside, all that may be called c language approach.

For example:

public native int hashCode();

public final native boolean compareAndSwapInt(Object paramObject, long paramLong, int paramInt1, int paramInt2);
复制代码

When this native method call, there will be native methods corresponding to the thread stack, this is actually similar to the java virtual machine stack. Also storing information of various native methods like local variable table.

There is a region, it is not jvm through the NIO allocateDirect this API, you can allocate memory space in the heap outer JVA, and then references the heap is operated by an outer java virtual machine memory space of the stack DirectByteBuffer.

Focus: Method area, the program counter, java java virtual machine stack and heap memory

Guess you like

Origin juejin.im/post/5d3bc362f265da1bc37f574c