...
Metaspace is a native (as in, off-java-heap) memory manager in the hotspot.
It is used to manage memory for class metadata. Class metadata are allocated when classes are loaded. Their lifetime is usually scoped to that of the loading classloader . When - when a loader gets collected, all class metadata it accumulated are released in bulk. Therefore it follows a bulk-delete scenario, and so the The memory manager does not need to track individual allocations for the purpose of freeing them. Hence, the metaspace allocator is an Arena- or Region-Based Allocator. It is optimized for fast, very memory efficient low-overhead allocation of native memory at the cost of not being able to (easily) delete arbitrary blocks.
...
Only one global context exists if compressed class pointers are disabled and we have no compressed class space. If compressed class pointers are enabled, we keep class space allocations separate from non-class space allocations. So we have two global metaspace contexts: one holding allocations of Klass structures (the "compressed class space"), one holding everything else (the "non-class" metaspace). Mirroring that duality, each CLD now owns two arenas as well.
![High Level Overview, No compressed class space disabled](./highlevel-overview-no-ccs.svg "High Level Overview, compressed class space disabled")![High Level Overview, compressed class space enabled](./highlevel-overview-ccs.svg "High Level Overview, compressed class space enabled"):
+--------+ +--------+ +--------+ +--------+
| CLD | | CLD | | CLD | | CLD |
+--------+ +--------+ +--------+ +--------+
| | | |
| | | | allocates variable-sized,
| | | | typically small-tiny metaspace blocks
v v v v
+--------+ +--------+ +--------+ +--------+
| arena | | arena | | arena | | arena |
+--------+ +--------+ +--------+ +--------+
| | | |
| | | | allocate and, on death, release-in-bulk
| | | | medium-sized chunks (1k..4m)
| | | |
v v v v
+--------------------------------------------+
| |
| Metaspace Context |
| (incl chunk freelist) |
| |
+--------------------------------------------+
| | |
| | | map/commit/uncommit/release
| | |
v v v
+---------+ +---------+ +---------+
| | | | | |
| virtual | | virtual | | virtual |
| space | | space | | space |
| | | | | |
+---------+ +---------+ +---------+
With compressed class space enabled, we have two Metaspace contexts (one normal, one wrapping the class space), and each CLD has now two arenas, one associated with non-class context, one with the class space.
+--------+ +--------+
| CLD | | CLD |
+--------+ +--------+
/ \ / \ Each CLD has two arenas...
/ \ / \
/ \ / \
v v v v
+--------+ +--------+ +--------+ +--------+
| noncl | | class | | noncl | | class |
| arena | | arena | | arena | | arena |
+--------+ +--------+ +--------+ +--------+
| \ / |
| --------\ | Non-class arenas take from non-class context,
| / | | class arenas take from class context
| /--------- | |
v v v v
+--------------------+ +------------------------+
| | | |
| Metaspace Context | | Metaspace Context |
| (nonclass) | | (class) |
| | | |
+--------------------+ +------------------------+
| | |
| | | Non-class context: list of smallish mappings
| | | Class context: one large mapping (the class space)
v v v
+--------+ +--------+ +----------------~~~~~~~-----+
| | | | | |
| virtual| | virt | | virt space (class space) |
| space | | space | | |
| | | | | |
+--------+ +--------+ +----------------~~~~~~~-----+
Core Concepts
Commit Granules
...