HotSpot JVM Memory Management
Automatic Memory Management
- Object Allocations
- Root Tracy
- Mark
- Collect Garbage
- Compact
- Allocations Continue
Generational Garbage Collection and Memory Spaces in the HotSpot JVM
-
Garbage collector is responsible for
- Memory allocation
- Keeping the referenced objects in memory
- Reclaiming the space used by unreachable objects
-
Unreachable objects are called garbage
-
This whole process of reclaiming memory is garbage collection
Generational Garbage Collection
-
Memory space is divided into generations
-
Separate pools holding objects of different age ranges
-
Based on hypothesis(假设):
- Most allocated objects die young
- Few references from older to younger objects exist
-
To take advantage of this hypothesis, heap is divided into two generations
- Young: small and collected frequently
- Old : larger and occupancy grows slowly
-
Minor(young) and Major(Full) collections
Young and Old Generation
Young | Old |
---|---|
Eden|Survivor From |Survivor To|Virtual | –|Virtual |
Metaspace
- JDK 8 does not have Permanent Generation
- Class metadata is stored in a new space called Metaspace
- Not contiguous with the Java Heap
- Metaspace is allocated out of native memory
- Maximum space available to the Metaspace is the available system memory
- This can though be limited by MaxMetaspaceSize JVM option
Compressed Class Space
-
If UseCompressedClassesPointers is enabled then two separate areas of memory are used for the classes and its metadata
- Metaspace
- Compressed class space
-
64-bit class pointers are represented with 32-bit offsets Class metadata referenced by the 32-bit offsets is stored in the Compressed Class Space
-
By default compressed class space is sized at 1GB
-
MaxMetaspaceSize sets an upper limit on the committed size of both of these spaces.
Java Heap | Metaspace |
---|---|
mark kclass | Klass Other metadata |
mark kclass | Klass Other metadata |
Compressed Class Space
Java Heap | Metaspace |
---|---|
mark kclass | Klass Klass |
mark kclass | Other metadata Other metadata |
Code Cache
- Code Cache is used to store the compiled code generated by the Just-in-time compilers
- It is allocated out of native memory
- Managed by the Code Cache Sweeper
Native Memory
- Available system memory
- Not managed by the JVM memory management
Summary: Section 2
- Memory space is divided into memory pools
- Java Heap
- Young generation
- Old generation
- Classes and metadata space
- Permanent Generation (before JDK 8)
- Metaspace(metaspace + compressed class space) (JDK 8 onwards)
- CodeCache
- Native Memory