Java memory partitioning:
In Java memory allocation, Java divides memory into: method area, heap, virtual machine stack, local method stack, and program counter. The method area and heap are shared by all threads, while the virtual machine stack, local method stack and program counter are thread-isolated. Each area has its own creation and destruction time.
Program counter:
It is used as a line number indicator of the bytes executed by the current thread. Java's multithreading is implemented by switching threads in turns and allocating processor execution time. Therefore, in order for each thread to return to its correct position after switching, each thread needs an independent program counter.
Java virtual machine stack:
When each is executed, a stack frame will be created at the same time to store local variable tables, operand stacks, dynamic links, method exits and other information. The virtual memory stack is what we often call the "stack". The memory required for the local variable table is allocated at compile time.
Local method stack:
Similar to the virtual machine stack, the difference is that the virtual machine stack executes Java method services for the virtual machine, while the local method stack serves the virtual machine using Native methods.
Java heap:
Shared by all programs and created when the virtual machine starts. This memory area is used to store object instances. According to the Java virtual machine regulations, the Java heap can be in physically discontinuous memory space as long as it is logically continuous.
Method area:
Same as the heap, shared between threads. Its function is to store data such as class information, constants, static variables, code compiled by the just-in-time compiler, etc. that have been loaded by the virtual machine.
Runtime constant pool:
Is part of the method area. Its function is to store various literal and symbol references generated during compilation.