The 5 major partitions and specific functions of the JVM in 6 minutes

In the process of executing java programs, the Java virtual machine divides the memory it manages into several different data areas, each of which has its own user-level creation and destruction time.

The heap area and the method area always exist with the startup of the virtual machine process.

The virtual machine stack, native method stack, and program counter are created and destroyed depending on the start and end of user threads.

insert image description here

heap [shared by all threads]

The heap area is the largest piece of memory managed by the JVM, and "almost" all object instances allocate memory here.

There are new generation and old generation in the heap area.

The new generation also includes 1 Eden area and 2 survivor areas [From survivor, To survivor],

The subdivision of the heap is for better allocation and deallocation of memory.

Method area [shared by all threads]

It is mainly used to store data such as loaded type information, constants, static variables, and code caches compiled by the just-in-time compiler.

In JDK8, the permanent generation inside has been discarded, and the metaspace of local memory is used instead, so that more type information can be loaded.

runtime constant pool

It is used to store various literal and symbol references generated during compilation.

Virtual machine stack [thread private]

This is what we often refer to as the stack area. When each method is executed, a stack frame is created synchronously, and each stack frame is used to store information such as local variable table, operand stack, dynamic link, and method exit.

The execution of each method is equivalent to pushing and popping the stack, and the data in the corresponding stack will be recycled after each method is executed.

So that is, some variables that we can define as local variables should be defined in local variables as much as possible to facilitate timely recovery.

local variable table

Stores data types, object references, and return address types known at compile time.

  • Basic data types [boolean, byte, char, short, int, float, long, double]

  • Object reference: The reference type is not equivalent to the object itself. It may be a reference pointer pointing to the starting address of the object, or it may point to a handle representing the object or other locations related to the object.

  • returnAddress type: points to the address of a bytecode instruction.

The storage controls of the above data types in the local variable table are represented by local variable slots (Slots), in which 64-bit long and double type data will occupy two variable slots, and other data types will only occupy one.

native method stack [thread-private]

The local method stack and the virtual machine stack are actually the same, the only difference is that this is provided for the local method call,
and the instruction value of the program counter when executing the local method is undefined.

program counter [thread private]

It is a small memory space, which can be regarded as an indicator of the bytecode line number executed by the current thread. [Some places are also called PC registers]

Some basic functions:
branches, loops, jumps, exception handling and thread recovery are all handled by it.

Each thread corresponds to its own unique program counter.

What the counter records is the address of the virtual machine bytecode instruction being executed, and the instruction to be executed next is selected by changing the value of the counter.

This is the only area where there is no OOM in the JVM specification.

Guess you like

Origin blog.csdn.net/admin_jalen/article/details/124206033