[JVM system learning road] run-time data area overview and program counter

[JVM system learning road] run-time data area overview and program counter

JVM system learning road series demonstration code address: github.com/mtcarpenter...

This article deals  and  knowledge point because it is not so much to sort out together, but also pave the way for the knowledge to make a follow-up study.

Overview of the runtime data area

The runtime data area is the stage after the class loading is complete. If you are not familiar with class loading, you can read my last article.

When we pass the previous   stages : class loading -> verification -> preparation -> parsing -> initialization , the execution engine will be used to use our class, and the execution engine will be used for our operation Time data area, the following memory is a very important system resource. It is an intermediate warehouse and bridge between hard disk and CPU. It carries the real-time running of the operating system and application programs. The JVM memory layout stipulates the memory application, allocation, and management of Java during operation. The strategy ensures the efficient and stable operation of the JVM. Different JVMs have some differences in memory division and management mechanisms. Combining with the JVM virtual machine specification, let's discuss the classic JVM memory layout.

The data we get through disk or network IO needs to be loaded into the memory first, and then the CPU gets the data from the memory for reading, which means that the memory acts as a bridge between the CPU and the disk

First of all, there is a complete picture of the runtime data area: The Java virtual machine defines several runtime data areas that will be used during the running of the program, some of which will be created when the virtual machine is started, and destroyed when the virtual machine exits. . Others have a one-to-one correspondence with threads, and these data areas corresponding to threads will be created and destroyed as the threads start and end.

The gray ones are private to a single thread, and the red ones are shared by multiple threads. which is:

  • Each thread: independently includes the program counter, stack, and local stack.
  • Sharing between threads: heap, off-heap memory (permanent generation or meta space, code cache)

Thread

A thread is a unit of operation in a program. JVM allows an application to be executed in parallel by multiple threads. In Hotspot JVM it, each thread is directly mapped to the native thread of the operating system.

  • When a Java thread is ready for execution, a native thread of the operating system is also created at the same time. After the execution of the Java thread terminates, the local thread will also be recycled.

The operating system is responsible for scheduling all threads to any available CPU. Once the local thread is initialized successfully, it will call the run() method in the Java thread.

JVM system thread

If you use the console or any debugging tool, you can see that there are many threads running in the background. These background threads do not include the calling public static void main String[]  main thread and all threads created by the main thread. These main background system threads Hotspot JVM  are mainly the following:

  • Virtual machine thread: The operation of this thread requires the JVM to reach a safe point before it appears. The reason these operations must occur in different threads is that they all require the JVM to reach a safe point so that the heap does not change. This type of thread execution includes "stop-the-world" garbage collection, thread stack collection, thread suspension, and biased lock cancellation.
  • Periodic task thread: This thread is the embodiment of time-period events (such as interrupts), and they are generally used for the scheduled execution of periodic operations.
  • GC thread: This thread provides support for different kinds of garbage collection behaviors in the JVM.
  • Compiler thread: This thread compiles bytecode into native code at runtime.
  • Signal scheduling thread: This thread receives the signal and sends it to the JVM, and processes it by calling the appropriate method within it.

Introduction to Program Counter (PC Register)

In the  ( Program Counter Register ) in JVM , the name of Register is derived from the register of the CPU, and the register stores the field information related to the instruction. The CPU can only run by loading data into 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. The program counter 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

PC  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.

for example

Demonstrate by writing a simple code:

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

Compile the above java file into a bytecode file, and then execute `javap -c PCRegisterTest.class``, check through the console, and find that there is a line number mark on the left side of the bytecode, which is actually the instruction address for Point to where the current execution is.

  public static void main(java.lang.String[]);
    Code:
       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

Two common problems

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.

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. The CPU will constantly switch tasks, which will inevitably lead to frequent interruptions or recovery. How to ensure that the scores are correct? PC  , In this way, each thread can perform independent calculations, so that there will be no mutual interference. Due to the limitation of the CPU time slice, many threads in the concurrent execution process, 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.

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. At the macro level: we 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. How to deal with fairness, one way is to introduce time slices, and each program is executed in turn.

summary

In this review, the first part is the runtime data area, and the second part is the program counter. JVM can run efficiently and stably, mainly because the memory layout of JVM stipulates the strategy of memory application, allocation, and management during Java operation. Next, we will describe the various areas under the runtime data area through many chapters. You also need to understand PC the address used to store the next instruction, which is also the instruction code to be executed. The execution engine reads the next instruction.


Welcome to public concern number mountains carpenter, I Xiaochun brother, in Java back-end development, will be a little front end, through continuous output series of technical articles to friends through literature, if this article can provide you with help, we welcome the attention, thumbs up, share support , See you next time!