Runtime data area

Program counter

The Program Counter Register is a small memory space that can be thought of as a line number indicator of the bytecode being executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to execute.

In order for threads to switch back to the correct execution position, each thread needs to have a separate program counter.

If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed. If the Native method is being executed, this counter value should be null. This memory region is the only one where the Java Virtual Machine Specification does not specify any OutOfMemoryError cases.

Java virtual machine stack

Like program counters, the Java Virtual Machine Stack is thread-private and has the same life cycle as a thread. The virtual machine Stack describes the thread-memory model of Java method execution: For each method execution, the Java virtual machine synchronously creates a Stack Frame [1] to store information about local variables, operand stacks, dynamic connections, method exits, and so on. The process of each method being called and executed corresponds to the process of a stack frame moving from the virtual machine stack to the virtual machine stack.

Local method stack

The Native method stack serves the Native methods used by the virtual machine.

The Java heap

The sole purpose of this memory area is to hold object instances, and almost all object instances, as well as arrays, should be allocated on the heap.

Methods area

It is used to store type information that has been loaded by the virtual machine, constants, static variables, just-in-time compiler compiled code cache, and so on.

Direct memory

The NIO (New Input/Output) class was introduced in JDK 1.4, introducing a Channel and Buffer based I/O method that can allocate off-heap memory directly using Native libraries. This is then referenced by a DirectByteBuffer object stored in the Java heap. This can significantly improve performance in some scenarios because it avoids copying data back and forth between the Java heap and Native heap.

Stack overflow and memory overflow

Stack overflow Out of memory
Program counter
Java virtual machine stack can can
Local method stack can can
The Java heap can
Methods area can
Direct memory can

reference

  • In-depth understanding of the Java virtual machine