In depth analysis of the use of Java memory areas

  • 2020-04-01 01:55:00
  • OfStack

Java memory partition:

      In Java memory allocation, Java divides memory into method areas, heaps, virtual machine stacks, local method stacks, and program counters. The method area and heap are Shared for all threads, while the virtual and local method stacks and program counters are isolated for threads. Each zone has its own creation and destruction time.

Program counter:

      Action is a line number indicator of the bytes being executed by the current thread. Multithreading in Java is achieved by switching threads in turn and allocating processor execution time. Therefore, in order for each thread to return to the correct position after switching, each thread needs a separate program counter.

Java virtual machine stack:

      Each stack frame is created at the time of execution to store local variables, operand stacks, dynamic links, method exits, and so on. A virtual memory stack is what we often refer to as a "stack." The required memory of local variables is allocated at compile time.

Local method stack:

      Similar to the virtual machine stack, the difference is that the virtual machine stack performs Java method services for the virtual machine, while the local method stack USES Native method services for the virtual machine.

The Java heap:

      Shared by all programs and created when the virtual machine starts. This memory area is used to hold object instances. According to the Java virtual machine, the Java heap can be in a physically discontinuous memory space, as long as it is logically continuous.

Methods area:

      Like the heap, it is Shared between threads. Function is to store the virtual machine has been loaded by the class information, constants, static variables, just-in-time compiler compiled code and other data.

Runtime constant pool:

      Is part of the method area. It stores the various literals and symbolic references generated at compile time.


Related articles: