In JDK1.8, HotSpot removed the method area (permanent generation) and changed it into a meta-space
Program counter
A program counter is a line number indicator of bytecode execution by the current thread. The bytecode interpreter works by changing the value of this counter to determine the next instruction to be executed. It is equivalent to the GOTO statement, which is an indicator of program control flow. Branches, loops, jumps, exception handling, thread recovery, and so on depend on this.
In order to restore the thread to the correct execution position after switching, each thread has an independent program counter. The counters of each thread do not affect each other and are stored independently. We call this kind of memory area “thread private” memory.
In summary, the program counter serves two purposes:
- Implement code flow control
- Records where the current thread is executing
The program counter is the only memory region where OutOfMemoryError does not occur
Java virtual machine stack
Like program counters, the Java virtual machine stack is thread-private and its lifecycle is synchronized with the thread. The Java virtual machine synchronously creates a Stack Frame for each method execution, which is used to store information about local variables, operand stacks, dynamic links, method exits, and so on. The call is ejected after completion.
- If the virtual machine stack memory size does not allow dynamic expansion, when the thread request stack depth exceeds the maximum depth, will throw
StackOverFlowError
- If it can be extended dynamically, it will be thrown if sufficient memory cannot be allocated when the stack is extended
OutOfMemoryError
Local method stack
This is similar to the role played by the virtual machine stack, except that the virtual machine stack executes Java methods while the Native method stack executes Native methods.
StackOverFlowError and OutOfMemoryError may also be thrown
The heap
The heap is the largest chunk of memory managed by the virtual machine and is an area shared by all threads. The only purpose is to hold object instances, and almost all objects are allocated in heap memory
The heap can also be divided into new generation and old generation in detail. The new generation can also be divided into Eden space, From Survivor, and To Survivor. Further partitioning is aimed at better memory reclamation and faster memory allocation.
Methods area
The method area, like the Java heap, is an area of memory shared by threads to store type information that has been loaded by the virtual machine, constants, static variables, just-in-time compiled code caches, and so on. Although the Java virtual machine describes the method area as a logical part of the Heap, it has a nickname called non-heap to distinguish it from the Java Heap.
The Java Virtual Machine specification only specifies the concept and role of a method area, not how to implement it. The relationship between the method area and the persistent generation is similar to the relationship between classes and interfaces in Java, and the persistent generation is an implementation of the method area by the HotSpot VIRTUAL machine. In JDK1.8, HotSpot completely removed the method area and changed it to a meta space, which uses direct memory.
Why do you want to change permanent substitution to a meta-space?
- Avoid OOM exceptions. Because PermSize and MaxPermSize are usually used to set the size of the permanent generation to determine the upper limit of the permanent generation, meta-space uses direct memory and is limited only by the natively available memory.
- More classes can be loaded. When using meta-space, the number of classes of metadata that can be loaded is no longer controlled by MaxPermSize, but by the actual available space on the system.
- To merge HotSpot and JRockit code, JRockit has never had such a thing as a permanent generation.