JVM memory and garbage collection series: program counter

Program counter

Introduction

In the Program Counter Register in the JVM, the name of Register is derived from the register of the CPU, and the register stores on-site information related to instructions. The CPU can only run by loading data into the registers. Here, it is not a physical register in a broad sense. Perhaps it is more appropriate to translate it into a PC counter (or instruction counter) (also called a program hook), and it is not easy to cause unnecessary misunderstandings. The PC register in the JVM is an abstract simulation of the physical PC register.

image-20200705155551919

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

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

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 executing, it is an undefned value (undefned).

It is an indicator of program control flow. Basic functions such as branches, loops, jumps, exception handling, and thread recovery all need to rely on this counter to complete. When the bytecode interpreter works, it selects the next bytecode instruction to be executed by changing the value of this counter.

It is the only area that does not specify any outotMemoryError conditions in the Java Virtual Machine Specification.

effect

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

image-20200705155728557

Code demo

We first write a simple code

public class PCRegisterTest {
    
    
    public static void main(String[] args) {
    
    
        int i = 10;
        int j = 20;
        int k = i + j;
    }
}

Then the code is compiled into a bytecode file, we check again and find that there is a line number identifier on the left side of the bytecode, which is actually the instruction address, used to point to where it is currently executed.

0: bipush        10
2: istore_1
3: bipush        20
5: istore_2
6: iload_1
7: iload_2
8: iadd
9: istore_3
10: return

Through the PC register, we can know the step of the current program execution image-20200705161007423

What is the use of using PC registers to store bytecode instruction addresses?

Because the CPU needs to constantly switch between threads, after switching back at this time, it has to know where to continue execution.

The bytecode interpreter of the JVM needs to determine what bytecode instruction should be executed next by changing the value of the PC register.

image-20200705161409533

Why is the PC register set as private?

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 constantly switch tasks, which will inevitably lead to frequent interruptions or recovery. How to ensure that the score is correct? In order 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 independent calculations can be performed between each thread, so that there will be no The situation of mutual interference.

Due to the limitation of the CPU time slice, in the concurrent execution of many threads, at any certain moment, a processor or a core in a multi-core processor can only execute one instruction in a certain thread.

This will inevitably lead to frequent interruptions or recovery, how to ensure that the points are not bad? 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.

image-20200705161812542

CPU time slice

The CPU time slice is the time allocated by the CPU to each program. Each thread is assigned a time period, which is called its time slice.

On the macro level: Russia can open multiple applications at the same time, and each program runs in parallel without contradiction.

But at the micro level: Since there is only one CPU, it can only process part of the program requirements at a time. One way to deal with fairness is to introduce time slices, and each program executes in turn.

image-20200705161849557

Guess you like

Origin blog.csdn.net/weixin_43314519/article/details/110410795