JVM memory model
The memory managed by the Java virtual machine consists of the following seven runtime data areas:
1. Program counter
(Program Counter Register)
- A small memory space, yes
As a line number indicator of the bytecode being executed by the current thread
Thread private
The memory of- It is worth noting that in the Java Virtual Machine Specification,
The only
aNo OutOfMemoryError cases are specified
Area!!
2. Java virtual machine stack
(VM Stack)
A thread-memory model for Java method execution
- Execute Java methods for the virtual machine (i.e
The bytecode
) serviceThread private
The memory of- its
The life cycle is the same as that of a thread
- The execution of each Java method corresponds to the loading and unloading of a stack frame
- Two types of exceptions:
- if
The stack depth of thread requests is greater than the depth allowed by the virtual machine
That will be thrownStackOverflowError
abnormal- if
The JVM stack size can be dynamically expanded
whenUnable to allocate enough memory for stack expansion
Is thrownOutOfMemoryError
abnormal
3. Local method stack
(Native Method Stacks)
- The difference in“Java Virtual Machine Stack” :
Local method stack
Only for theNative methods used by virtual machines
Service that provides an in-memory environment for its execution- with“Java Virtual Machine Stack”The same,
Local method stack
There are also two types of exceptions:
Stack depth overflow
Is thrownStackOverflowError
abnormalStack extension failure
Is thrownOutOfMemoryError
abnormal
4. The Java heap
(Java Heap)
The largest chunk of memory managed by a virtual machine
The Java heap
isShared by all threads
An area of memory- Sole purpose:
Example of Storing objects
.
- In Java“Almost”This is where all object instances are allocated memory;
- But, because of current technological developments, said“Java object examples are all allocated on the heap” is also becoming less absolute.
The Java heap
isGarbage collector
Managed memory area, also known as"GC heap"
- The Java heap can be in a physically discontinuous memory space, but
Logically it should be considered continuous
.- If the
There is no memory in the Java heap to complete the instance allocation
And,The Java heap can no longer be extended
, the Java VIRTUAL machine will throwOutOfMemoryError
abnormal
5. Methods area
Method (Area)
- and“Java heap”As the
Shared by all threads
An area of the city.- In the Java Virtual Machine Specification, the
The method area is described as a logical part of the heap
But it has a nickname called“Pile”, the purpose is withThe Java heap
Distinguish.- if
The method area cannot meet the new memory allocation requirements
Is thrownOutOfMemoryError
abnormal
6. Run-time constant pool
(Running Constant Pool)
Run-time constant pool
isPart of the method area
- Constant pool table: Used for storage
Compile time
Generated varietyliteral
withCharacter references
.
- This section will be in the
After the class loading
Deposit toRun-time constant pool for the method area
In the.Run-time constant pool
The relativeClass file constant pool
An important feature of theDynamic
.- when
The constant pool could not be allocated to memory
Is thrownOutOfMemoryError
abnormal
7. Direct memory
(Direct Memory)
- It is neither part of the run-time data area of the virtual machine nor an area of memory as defined in the Java Virtual Machine Specification.
- But this area of memory is also frequently used and can cause
OutOfMemoryError
unusual
- in
JDK 1.4
New inNIO (New Input/Output)
Class, introducedAn I/O method based on Channel and Buffer
, it can be usedNative libraries
Direct allocation of off-heap memory
And then throughA DirectByteBuffer object stored in the Java heap
As aA reference to this block of memory
Perform operations. suchCan significantly improve performance in some scenarios
Because theIt avoids copying data back and forth between the Java heap and Native heap
.- Native direct memory allocation is not affected
The Java heap
Size limitation, however, since it is memory, it is definitely still subject toNative total memory
(including RAM and SWAP area or paging file) size andProcessor addressing space
Restrictions. The server administrator configures VM parameters based on the actual memory-Xmx
And other parameters, but are often ignoredDirect memory
, so that the sum of each memory area is greater than the physical memory limit (both physical and operating system level limits), resulting inThe dynamic extension
whenOutOfMemoryError
The exception.