JVM memory and garbage collection series: run-time data area overview and threads

Runtime data area overview and threads

Preface

This section mainly talks about the runtime data area, which is the part of the figure below, which is the stage after the completion of the class loading

image-20200705111640511

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

image-20200705111843003

That is, the chef cooks. We compare the things behind the chef (cut dishes, knives, seasonings) to the runtime data area. And the chef can be compared to the execution engine, making exquisite dishes by preparing things

image-20200705112036630

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 strategy of Java in the running process, ensuring the JVM Efficient and stable operation. Different JVMs have some differences in memory division methods and management mechanisms. Combined 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.

Complete picture of the runtime data area

image-20200705112416101

The Java virtual machine defines several runtime data areas that will be used during the running of the program, some of which are 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)

image-20200705112601211

Thread

A thread is a unit of operation in a program. JVM allows an application to be executed in parallel by multiple threads.
In the Hotspot JVM, 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 main thread that calls public static void main (String[]) and all threads created by the main thread. |
These main background system threads are mainly the following in Hotspot JVM:

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

Guess you like

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