Thread private: program counter virtual machine stack local method stack

Thread shared: heap method area direct memory (part of non-runtime data area)

1. Program counter

A program counter is a small area of memory that can be thought of as an indicator of the line number of bytecode executed by the current thread. The bytecode interpreter changes the value of this counter to select the next bytecode instruction to be executed. Branches, loops, jumps, exception handling, thread recovery and other functions rely on this counter to complete.

In addition, in order to restore the correct execution position after the thread switch, each thread needs to have an independent program technology, counters between threads do not affect each other, independent storage, we call this kind of memory area “thread private” memory.

Function:

(1) Bytecode interpreter reads instructions in turn by changing the program counter, so as to realize the process control of the code, such as sequential execution, selection, circulation and exception handling.

(2) In the case of multiple threads, the program counter is used to record where the current thread is executing, so that when the thread is switched back, it can know where it was last run.

Note: The program counter is the only memory area that does not appear in OOM, and its life cycle is created with the creation of the thread and dies with the end of the thread.

2. Java VM stack

Thread private, which describes the in-memory model of Java method execution, where the data for each method call is passed through the stack. In fact, the virtual machine stack is composed of a stack frame, each stack frame has: local variable table operand stack dynamic link method exit and other information.

(Boolean byte char short int float long double) (Boolean byte char short int float long double) It could also be a handle to an object or some other location associated with that object.)

There are two types of errors in the virtual stack: StackOverFlowError and OOM

StackOverFlowError: A StackOverFlowError is thrown when a thread requests a stack depth that exceeds the maximum depth of the current Java virtual machine stack if the memory size of the stack does not allow dynamic scaling

OutOfMemoryError: The memory size of the Java VM stack can be dynamically expanded. If the VM cannot obtain sufficient memory space during the dynamic expansion, an OOM is thrown

Extension: So how do methods/functions get called?

The Java stack can be compared to the stack in the data structure. The main content stored in the Java stack is the stack frame. Each function call will have a corresponding stack frame pushed into the Java stack, and after each function call, a stack frame will be popped up.

Either way, the return statement throws an exception that will cause the stack frame to be ejected.

3. Local method stack

This function is similar to that of the virtual machine stack, except that the virtual machine stack performs Java methods (that is, bytecode) services for the virtual machine, while the Native method stack serves Native methods used by the virtual machine.

4. The heap

Threads share object instances, where almost all object instances and arrays are stored in memory. Since most collectors now use generational collection algorithms, the Java heap can be subdivided into “new generation” and “old generation”, and further divided into “Eden space”, “From Survivor”, and “To Survivor space” for better memory reclamation or faster memory allocation.

After a new generation garbage collection, if the object is still alive, it will enter S0 or S1, and the age of the object will be increased by 1(the initial age of the object after Eden ->Survivor zone becomes 1). When its age increases to a certain level (default is 15 years old), Will be promoted to the old age. The object is promoted to the old age threshold by parameter

-xx :MaxTenuringThreshold.

The heap is most prone to OOM errors

(1) OOM: GC overHead Limit Exceeded: This error occurs when the JVM spends too much time performing garbage collection and can only reclaim too little heap space

This error occurs if there is not enough memory space in the heap to hold the newly created object. The -xmx setting

5. Methods area

Thread sharing. It is used to store information about classes that have been loaded by the virtual machine, constants, static variables, code compiled by the just-in-time compiler, and so on.