Quick revision
The short answer
Metaspace holds class metadata: the JVM's internal structures for each loaded class, method data, bytecode, constant pools and related runtime data. Unlike PermGen, which was part of the heap reservation with a fixed maximum, metaspace is native memory and is unbounded by default, while interned strings and static fields now live on the heap. MaxMetaspaceSize caps it, MetaspaceSize sets the level at which growth first triggers a GC to unload classes, and CompressedClassSpaceSize sizes the separate area that holds class structures when compressed class pointers are on.
What to remember
In this answer, we saw that metaspace holds class metadata such as class structures, bytecode, constant pools and method data, in native memory, while interned strings and static fields moved to the heap starting in JDK 7. Unlike PermGen, metaspace has no limit by default; MaxMetaspaceSize caps it and MetaspaceSize sets the first level at which growth triggers class unloading. With compressed class pointers, Klass structures go into a separate compressed class space sized by CompressedClassSpaceSize, which has its own out-of-memory error.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Metaspace holds class metadata: the JVM's internal structures for each loaded class, method data, bytecode, constant pools and related runtime data. Unlike PermGen, which was part of the heap reservation with a fixed maximum, metaspace is native memory and is unbounded by default, while interned strings and static fields now live on the heap. MaxMetaspaceSize caps it, MetaspaceSize sets the level at which growth first triggers a GC to unload classes, and CompressedClassSpaceSize sizes the separate area that holds class structures when compressed class pointers are on.
Why this question is asked
Every Java application loads thousands of classes, and frameworks such as Spring and Hibernate generate more at runtime. Where their metadata lives decides which error we see when something goes wrong, and which flag fixes it. Interviewers ask this to check that we know what changed in Java 8 and why old advice about PermGen no longer applies. In this answer, we will recap what class metadata is, see what moved where when PermGen went away, look at the flags that control metaspace, and finish with the compressed class space and its own error.
1What Class Metadata Is
When the JVM loads a class, it reads the class file and builds internal structures that describe it: the class layout and its fields, the methods with their bytecode, the constant pool with the names and literals the code refers to, and data the JIT collects while the code runs, such as how often each branch is taken. This is class metadata, and it lives in metaspace.
Metadata belongs to the class loader that loaded the class. Metaspace allocates it in chunks per class loader, so that when a class loader becomes unreachable and its classes are unloaded, all their metadata can be freed in one go.
2What Moved When PermGen Went Away
Before Java 8, class metadata lived in the permanent generation, PermGen, a fixed-size area that was reserved next to the heap and sized with MaxPermSize. Interned strings and static fields lived there too. Applications with many classes, or servers that redeployed web applications several times, would fill it and fail with OutOfMemoryError: PermGen space, even when the heap had plenty of room.
The move happened in steps. In JDK 7, interned strings moved to the ordinary heap, and static fields moved into the java.lang.Class object, the class mirror, which is also on the heap. In JDK 8, PermGen was removed and the remaining metadata moved to metaspace in native memory, outside the Java heap.
| Data | Java 6 and earlier | Java 8 and later |
|---|---|---|
| Class structures, bytecode, constant pools | PermGen | Metaspace (native memory) |
| Interned strings | PermGen | Heap (since JDK 7) |
| Static fields | PermGen | Heap, in the Class mirror |
| Limit by default | Fixed MaxPermSize | None for metaspace |
3The Flags That Control Metaspace
Because metaspace is native memory, by default it grows as long as the operating system gives it memory. MaxMetaspaceSize sets a hard cap; reaching it gives OutOfMemoryError: Metaspace, which is a clear signal of too many classes or a class loader leak.
MetaspaceSize is often misunderstood. It is not the initial size of anything; it is the first high-water mark. When committed metaspace reaches it, the JVM runs a GC to unload unused classes and then raises the mark. On 64-bit HotSpot it starts at about 21 MB, so large applications run a few such GCs during startup. Setting it higher, for example to the size the application reaches after startup, avoids those early collections.
Notice that jcmd VM.metaspace splits usage into Class and Non-Class. That split comes from the compressed class space.
4The Compressed Class Space
On 64-bit HotSpot, every object header points to its class. To keep that pointer at 4 bytes, compressed class pointers are on by default, and they only work if all class structures, the Klass objects, sit inside one contiguous area. So metaspace is split into two parts: the compressed class space for Klass structures, and the rest of metaspace for everything else, such as bytecode and constant pools.
The compressed class space has a fixed reserved size, set by CompressedClassSpaceSize and 1 GB by default. It is reserved at startup and committed only as needed, so the reservation costs address space, not real memory.
- Java heapObjects, interned strings, Class mirrors with static fields
- Compressed class spaceKlass structures; fixed reservation,
CompressedClassSpaceSize - Non-class metaspaceBytecode, constant pools, method data; grows on demand
- Operating system memoryBoth metaspace parts are native memory
4 layers, from the top down. Step through them one at a time.
MaxMetaspaceSize limits both metaspace parts together; the class space also has its own limit.
5Follow-up questions
What does OutOfMemoryError: Compressed class space mean?
It means the area that holds Klass structures is full, even though overall metaspace may still have room. It happens when an application loads a very large number of classes, often because of a class loader leak or code that generates classes per request, such as dynamic proxies or scripting engines. We first check with jcmd VM.metaspace or a heap dump whether the class count keeps growing. If it does, we fix the leak. If the class count is legitimately high, we raise CompressedClassSpaceSize; the maximum depends on the JDK version, so check yours.
Should MaxMetaspaceSize always be set?
In containers I would set it, well above the size the application reaches after warmup, for example two to three times that size. Without it, a class loader leak grows native memory until the kernel kills the process, which gives no Java error and no hint of the cause.
Can classes be unloaded?
Yes, but only all together with their class loader. When a class loader and every class it loaded become unreachable, a GC can unload them and free their metadata. Classes loaded by the application class loader are effectively never unloaded.
6Conclusion
In this answer, we saw that metaspace holds class metadata such as class structures, bytecode, constant pools and method data, in native memory, while interned strings and static fields moved to the heap starting in JDK 7. Unlike PermGen, metaspace has no limit by default; MaxMetaspaceSize caps it and MetaspaceSize sets the first level at which growth triggers class unloading. With compressed class pointers, Klass structures go into a separate compressed class space sized by CompressedClassSpaceSize, which has its own out-of-memory error.
Open with
Metaspace is where HotSpot keeps class metadata: the internal structure of each loaded class, its methods and bytecode, constant pools, and runtime data like method profiles. It is allocated per class loader, so when a class loader is unloaded its metadata is freed together.
Then, if they want more
The big difference from PermGen is location and limits. PermGen was a fixed-size area reserved alongside the heap and sized with MaxPermSize, so apps with many classes or frequent redeploys hit OutOfMemoryError PermGen space. Metaspace is native memory and grows on demand with no limit by default.
Also, not everything moved there: interned strings moved to the heap in JDK 7, and static fields live in the Class mirror object on the heap. For flags, MaxMetaspaceSize sets a hard cap, which I would set in containers so a class loader leak fails with OutOfMemoryError Metaspace instead of an OOM kill.
MetaspaceSize is the first high-water mark: when metaspace reaches it, the JVM runs a GC to unload classes and raises the mark, so setting it higher avoids GCs during startup. And with compressed class pointers, the Klass structures go into a separate compressed class space sized by CompressedClassSpaceSize, 1 GB by default, which has its own error when it fills.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What lives in metaspace, how is it different from the PermGen used before Java 8, and...": https://scrivio-interview-library.netlify.app/java/memory-layout/6e8796c69b/