Local method stack and PC register

Local method stack and PC program counter

One, the local method stack

1. The Java virtual machine stack is used to manage the calls of Java methods, and the local method stack is used to manage the calls of local methods (generally non-Java implemented methods)

2. The local method stack is also private to the thread .

3. Allow to be implemented as a fixed or dynamically expandable memory size . (The situation is the same as the Java virtual machine stack in terms of memory overflow)

  • If the stack capacity allocated by the thread request exceeds the maximum capacity allowed by the local method stack, the Java virtual machine will throw a StackOverFlowError exception.
  • If the local method stack can be dynamically expanded, and cannot apply for enough memory when trying to expand, or there is not enough memory to create the corresponding local method stack when creating a new thread, then the java virtual machine will throw an OutOfMemoryError abnormal.

4. The native method is implemented in C language

5. Its specific method is to register the native method in the Native Method Stack, and load the native method library when the Execution Engine is executed.

6. When a thread calls a local method, it enters a new world that is no longer restricted by the virtual machine. It has the same permissions as the virtual machine

  • The method may be by local native method interface to access virtual machines running inside the data area
  • It can even directly use the registers in the local processor
  • Allocate any amount of memory directly from the heap of local memory

7. Not all JVMs support native methods. Because the Java virtual machine specification does not explicitly require the language, specific implementation, data structure, etc. of the local method stack. If the JVM product does not plan to support native methods, there is no need to implement a native method stack.


2. PC program counter

  • pc register
  • Program Counter Register
  • Each thread has a program counter, which is private to the thread, which is a pointer to the method bytecode in the method area (used to store the address of the next instruction, that is, the instruction code to be executed)
  • Very small space-negligible

Insert picture description here

  • Note: The native method is not managed by java, so the counter is empty

The PC register is used to store the address that points to the next instruction, that is, the instruction code to be executed. The execution engine reads the next instruction.

1. It is a small memory space, almost negligible. It is also the fastest storage area

2. In the JVM specification, each thread has its own program counter, which is private to the thread, and the life cycle is consistent with the life cycle of the thread

3. There is only one method in a thread at any time, which is the so-called current method . The program counter stores the JVM instruction address of the java method being executed by the current thread; or, if the native method is actually executed, it is an undefined value (undefined), because the program counter is not responsible for the local method stack.

4. It is an indicator of program control flow. Basic functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on this counter to complete

5. When the bytecode interpreter works, it selects the bytecode instructions that need to be executed by changing the value of this counter.

6. It is the only area that does not specify any OOM (Out Of Memery) conditions in the Java virtual machine specification , and there is no garbage collection

Use javap -v xxx.class to decompile the bytecode file and view information such as instructions

Insert picture description here
Insert picture description here

The bright color in the above picture has two characteristics:

  • Shared by all threads ( gray is thread private )
  • There is garbage collection in brightly colored places

3. Frequently Asked Questions about PC Register Interview

1. What is the use of using the PC register to store the bytecode instruction address (why use the PC register to record the execution address of the current thread)

(1) Multithreading is macroscopically parallel (multiple events occur at the same time at the same time), but in fact it is executed concurrently and alternately

(2) Because the CPU needs to constantly switch between threads, after switching back at this time, you have to know where to continue execution

(3) The bytecode interpreter of the JVM needs to change the value of the PC register to clarify what bytecode instruction should be executed next

Therefore, in the process of concurrent execution of many threads, at any certain moment, a processor or a core in a multi-core processor will only execute one instruction in a certain thread. This will inevitably lead to frequent interruptions or recovery, how to ensure that there is no difference in points? After each thread is created, it will generate its own program counter and stack frame, and the program counter does not affect each other among the threads.

2. Why is the PC register set as thread private?

(1) We all know that the so-called multi-threading method will only execute one of the threads in a specific period of time, and the CPU will continue to do task switching, which will inevitably lead to frequent interruptions or recovery. How to ensure that the points are correct? ?

(2) In order to be able to accurately record the current bytecode instruction address being executed by each thread, the best way is naturally to ** allocate a PC register for each thread, ** so that each thread can perform Calculate independently, so there will be no mutual interference.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43803285/article/details/115283158