Memory Model of Java Virtual Machine

program counter

Can be thought of as a line number indicator of the bytecode being executed by the current thread. In the conceptual model of the virtual machine, the bytecode interpreter selects the next bytecode instruction to execute by changing the value of this counter when it works. Basic functions such as branching, looping, jumping, exception handling, and thread recovery all require One come to this counter to complete. This memory region is the only region that does not specify any OutOfMemoryError conditions in the Java Virtual Machine Specification.

Java virtual machine stack

The virtual machine stack describes the memory model of Java method execution: each method will create a stack frame to store local variable table, operand stack, dynamic link, method exit and other information while executing. The process of each method from invocation to completion of execution corresponds to the process of pushing a stack frame to popping out of the stack in the virtual machine.

The stack memory we usually talk about generally refers to the local variable table. The local variable table stores various basic data types, object references and returnAddress (pointing to the address of a bytecode instruction) known at compile time. Among them, 64-bit long and double type data will occupy 2 local variable spaces (Slot), and the other data types only occupy one. The memory space required by the local variable table is allocated during compilation. When entering a method, how much local variable space this method needs to allocate in the stack frame is completely determined, and the size of the local variable table will not be changed during method execution. .

Two exceptions to the virtual machine stack:

  1. The stack depth requested by the thread is greater than the depth allowed by the virtual machine, and a StackOverflowError exception will be thrown.
  2. When the virtual machine dynamically expands, it cannot apply for enough memory, and an OutOfMemoryError exception is thrown.

native method stack

Similar to the virtual machine stack, the difference between them is that the virtual machine stack executes Java methods, while the native method stack serves the Native methods used by the virtual machine. Like the virtual machine stack, the native method stack area also throws StackOverflowError and OutOfMemoryError exceptions.

Java heap

For most applications, the Java heap is the largest chunk of memory managed by the virtual machine. This memory area is shared by all threads and is created when the virtual machine starts. The sole purpose of this area is to hold object instances. Almost all object instances allocate memory here (not all). The Java heap is the main area of ​​garbage collection management. From the perspective of recycling, the Java heap can be subdivided into: the new generation, the old generation, and the more detailed ones are Eden, From Survivor, and To Survivor.

method area

It is used to store data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970247&siteId=291194637