"Five minutes a day, JVM Fun": Summary of runtime data area

Outline

We first come to know about JVMthe runtime data area, if JVMa person, then the runtime data area is this person's skeleton, which supports the JVM to run, so let's learn disaggregated data area at runtime and brief .

classification

From the point of view where the area can be divided into internal memory and external memory, internal memory which also includes the exclusive district of threads and threads share.

Inside the JVM, from the point of view shared by the threads, the thread can be divided into exclusive areas and threads share.

Thread exclusive region may comprise three data areas are:

  • Program Counter (Program Counter Register)

  • Virtual Machine stack (VM Stack)

  • Native method stacks (Native Method Stack)

Thread shared data area contains the following two areas:

  • Method region (Method Area)
  • Heap (Heap)

In the JVM that is outside the local memory in, including direct memory and metadata (the Metadata) , in JDK 1.8, the metadata is to our previous generation of permanent (permanent generation)

The difference between them is, JDK 7 in the constant pool is moved to permanent generation heap memory in JDK 8, moved the remainder of the metadata.

The principle of multi-threaded

Above we run the data area is divided into an exclusive area of ​​the thread and thread shared area, so multi-threaded Java is how to achieve it? This can help us have a better understanding of the area and share a thread exclusive area.

When multiple threads running, in fact, is to use the CPU time is divided into numerous small parts, then go to these threads allocated time, CPU time to quickly switch these small blocks based on priority, to the user experience is more threads running simultaneously, as shown below:

By this figure, we can clearly see how to achieve the effect of multi-threaded (In fact, when communication has the same principle - Time Division Multiplexing)

In fact, plainly, the thread exclusive area primarily to control the normal operation method , and shared area thread is more akin to a warehouse to store information .

Make a simple analogy, now there is a method we use two threads to call this method, the information belongs to the method can be called exclusive area, and class variables, objects, but the two methods can be used simultaneously .

Two kinds of abnormal

In the process of dealing with and JVM, we often encounter two errors: StackOverflowErrorand OutOfMemoryError,

StackOverflowErrorAn exception occurred in native method stacks and stacks virtual machine thread exclusive zone, but OutOfMemoryErrorwill appear in all regions except the program counter.

No public

Original articles, ignorant, if imperfect, Wan Wang told!

Guess you like

Origin juejin.im/post/5d5e30ae5188253da24d41e9