Detailed memory

RAM

Jvm memory is divided into method area, counter, local method stack, stack memory, heap memory
Insert picture description here

Stack: No matter what type, as long as it is pushed into the stack, the basic type is the stack type

counter

​ In order to make each thread work normally, the Program Counter Register is proposed. Each thread has its own program counter so that when the thread is switched, it can continue to execute on the basis of the previous execution, only from one From the perspective of linear execution of threads, the code is executed one by one, which is the program counter at this time; the JVM determines the next bytecode instruction to be executed by reading the value of the program counter, and then performs the selection statement, Loop, exception handling, etc.;

​ A thread is executing the thirty-fifth line of HelloWorld.class. At this time, the CPU time slice is taken away by the B thread. When the A thread is re-allocated to the time slice, how does he know where my class is running? At this time he can see where the program counter is.

​ The program counter is born for multi-threading, and it is completely unnecessary in the case of single-threaded. It is not difficult to find from the case that the program counter is unique to each thread, not shared by threads, so it is thread safe!

Native method stack

Refers to the stack of methods to run the operating system

Thread

When each thread starts, a new thread stack space is created in the stack memory. The main method is first pushed onto the stack, and then executed sequentially. Each time a method is executed, the method is pushed onto the stack, after the execution is completed, the method is popped off the stack, and finally When the execution is complete and all methods are popped out of the stack, the thread is executed. When the class is instantiated, the member variables and methods do not involve the order of execution, only the order of initialization, so the member variables and methods of the class are in the heap memory.

That is, the handle information of the variables declared outside the method is in the heap memory, and the handle information of the variables declared inside the method is in the stack memory. The static is in the method area

The value of the basic type exists along with the handle information, and the value of the reference type is in the heap memory

128 traps

Quote blog: https://blog.csdn.net/zjs_scallop/article/details/103035113

Guess you like

Origin blog.csdn.net/Walker7143/article/details/106072581