1. Overview of the runtime data area

Memory is a very important system resource. It is the intermediate warehouse and bridge between hard disk and CPU, carrying the operating system and application program running in real time. The JVM memory layout defines the Java memory allocation, allocation, and management strategies during the running process, ensuring efficient and stable running of the JVM. There are some differences in how memory is divided and managed by different JVMS. Take a look at the classical JVM memory layout in conjunction with the JVM virtual machine specification.

The Java virtual machine defines several run-time data areas that are used by programs while they are running, some of which are created when the virtual machine is started and destroyed when the virtual machine exits. Others are thread-specific, with thread-specific data areas created and destroyed as threads start and end.

  • Thread private: program counters, stacks, local stacks
  • Thread sharing: heap, off-heap memory (permanent generation or meta-space, code cache)

Please refer to the official linkDocs.oracle.com/javase/spec…

1.1 the Error versus the GC

Error GC
Program counter x x
The virtual machine stack Square root x
Local method stack Square root x
The heap Square root Square root
Methods area Square root Square root

Thread 2.

  • Threads are the unit of execution within a program, and the JVM allows an application to have multiple threads executing in parallel
  • In Hotspot JVM, each thread maps directly to the operating system’s native thread
    • When a Java thread is ready to execute, an operating system native thread is created at the same time. When Java thread execution terminates, the local thread also recycles
  • The operating system is responsible for scheduling all threads to any available CPU. Once the local thread initializes successfully, it calls the run() method in the Java thread
  • Using JConsole or any debugging tool, you can see many threads running in the background. These background threads do not include the main thread that calls public static void main(String[] args) and all threads created by the main thread; The main backend threads in the Hotspot VIRTUAL machine are:
    • Virtual machine threads: Thread operations do not occur until the JVM has reached a safe point. The reason these operations must occur in different threads is that they all need the JVM to reach a safe point so that the heap does not change. The execution types of this thread include “stop-the-world” garbage collection, thread stack collection, thread suspension, and partial lock cancellation;
    • Periodic task threads: These threads are representations of periodic events (such as interrupts) and are used for scheduled execution of periodic operations.
    • GC thread: This thread provides support for different kinds of garbage collection behavior within the JVM;
    • Compilation thread: this thread compiles bytecode to local code at run time;
    • Signal scheduling thread: This thread receives signals and sends them to the JVM for processing within it by calling the appropriate methods

3. Program Counter (PC Register)

3.1 PC Register Introduction

The Program Counter Register in the JVM is named after the CPU Register, which stores instruction related field information. The CPU can only run if it loads data into a register. Instead of referring to physical registers in a broad sense, it might be more appropriate to translate them as PC counters (or instruction counters) (also known as program hooks). The PC register in the JVM is an abstract simulation of the physical PC register.

  • It’s a small, almost negligible amount of memory. It is also the fastest storage area
  • In the JVM specification, each thread has its own program counter, which is thread-private and whose lifetime is consistent with that of the thread
  • There is only one method executing per thread at any one time, which is called the current method. The program counter stores the JVM instruction address of the Java method being executed by the current thread; Or, if you are executing a native method, undefined
  • It is an indicator of program control flow, and basic functions such as branching, looping, jumping, exception handling, thread recovery, and so on rely on this counter
  • The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to execute
  • It is the only area where the Java Virtual Machine specification does not specify any OutOfMemoryError cases

3.2 role

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

3.3 Code Demo

public class PCRegisterTest {
    public PCRegisterTest(a) {}public static void main(String[] args) {
        int i = 10;
        int j = 20;
        intvar10000 = i + j; }}Copy the code

Partial output is as follows:

      stack=2, locals=4, args_size=1
         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
Copy the code

The number in the front column represents the instruction address (offset address), which is the actual data stored in the PC memory machine, and the operation instruction in the back column. The execution engine takes instructions from locations represented by data stored in PC registers; The execution engine manipulates the local variable table, operand stack, and translates bytecode instructions into machine instructions.

3.4 the problem

3.4.1 What is the use of using PC registers to store byte code instruction addresses

Because the CPU is constantly switching threads, when it comes back, it needs to know where to start to continue execution. The JVM bytecode interpreter needs to change the value of the PC register to determine what bytecode instructions to execute next.

3.4.2 Why are PC registers set to thread private

We all know the so-called multi-threading in a specific period of time will only execute one of the methods of a thread, CPU will keep doing task switching, so inevitably lead to frequent interruptions or skin care, how to ensure that there is no difference? In order to keep an accurate record of the current address of the bytecode instructions being executed by each thread, it is naturally best to assign each thread a PC register so that each thread can compute independently without interfering with each other.

3.5 CPU Time slice

The CPU time slice is the time that the CPU allocates to each program. Each program is allocated a time segment, called its time slice. On a macro level, we can have multiple applications open at the same time, each running in parallel. But on a micro level, since there is only one CPU, which can only handle a portion of the program’s requirements at a time, one way to deal with fairness is to introduce time slices, which each program executes in turn.