Java Virtual machine memory
Java Virtual machine memory, also known as Java memory, can be understood as the Java Virtual Runtime data area.
JVM Memory Structure
Includes: Thread-shared method areas and heaps, and each thread's private Java stack, local method stack, and PC counter (program counter).
Java heap
Heap, which is the largest chunk of memory managed in a Java virtual machine, this memory area The only purpose is to store object instances and Arrays .
All the memory of objects created by new is allocated in the heap, and its size can be controlled by-XMX and-XMS.
The origin of Java heap and garbage collection
The Java heap is also the main area of garbage collector management, and is also known as the "GC heap".
From the memory recycling point of view, because the current collector basically uses the Generational collection algorithm, so the Java heap can also be subdivided into: Cenozoic and Laosheng generation .
A more detailed breakdown is as follows: The Cenozoic is further divided into Eden and from Survivor, and the last from survivor by Fromspace and
Tospace composition.
- Cenozoic : new objects are used to allocate memory in the Cenozoic, Eden Space is not enough, the surviving objects will be transferred to the survivor, the Cenozoic large and small can be controlled by-XMN, You can also use-xx:survivorratio to control the proportions of Eden and survivor.
- old generation : used to store a new generation of many the object that the secondary garbage collection is still alive.
Java Virtual Machine specification rules for Java heap
The Java heap can be in a physically disjoint memory space, as long as it is logically contiguous. When implemented, it is possible to achieve a fixed size,
can also be is extensible, but the current mainstream virtual machines are implemented in a scalable way (via-XMX and-xms control).
About outofmemoryerror exceptions:
A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.
PS:outofmemoryerror sometimes referred to as OOM exception
Java stack (also called Virtual machine stack)
Each thread executes each method by applying a stack frame in the stack, each of which includes the local variable area and the operand stack, which is used to store this method
Temporary variables, parameters, and intermediate results in the call procedure.
Note : The Java stack is for the Java Method service, and the local method stack is for the native method.
In the Java Virtual Machine specification, there are two exceptions to this area of the Java stack:
- If the thread requests a stack depth greater than the virtual machine allows, Stackoverflowerrorwill be thrown.
- If the Java stack can be dynamically extended, but the extension cannot request enough memory, the OutOfMemoryError exception is thrown.
As with the Java stack, the local method stack area throws stackoverflowerror and outofmemoryerror exceptions.
The Ps:native method is a method written in another language that can only be called in Java.
Local method Stack
Used to support the execution of the native method, which stores the state of each native method call.
Method area
The method area, like the Java heap, is an area of memory shared by each thread.
Contains the class information to load, static variables, constants of the final type, properties, and method information. JVM with persistent generation (permanetgeneration)
To store the method area, you can specify the minimum and maximum values by-xx:permsize and-xx:maxpermsize.
The Java Virtual machine specification describes the method area as a logical part of the heap, but it has an alias called Non-heap (Not a heap), which is intended to be
The Java heap is distinguished.
Jvm-java Memory Structure