Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Inside the JVM

    Runtime Memory Areas

    Metaspace vs PermGen: before Java 8, class metadata lived in a fixed-size heap area called PermGen, famous for OutOfMemoryError: PermGen space.

    Chapter 3 of 106 min readOften asked in interviews

    The Plain Picture#

    • The stage (shared by everyone): all the props and sets in use — the heap.

    • The script library backstage (shared): the scripts describing every scene — metaspace.

    • Each actor's personal notebook (private): what they're doing right now and where they are in the script — stack and PC register.

    The Real Names#

    AreaShared?HoldsError when full
    Heapall threadsevery object and array (and their instance fields)OutOfMemoryError: Java heap space
    Metaspaceall threadsclass metadata: class structures, method bytecode, the runtime constant pool. (Static fields are not here: since Java 8 they live on the heap, inside the class's java.lang.Class object)OutOfMemoryError: Metaspace
    JVM stackone per threadstack frames: local variables (primitives and references), operand stack, return addressStackOverflowError
    PC registerone per threadthe address of the bytecode instruction being executed (the JVM's version of the CPU's program counter — How Computers Work Ch 5)—
    Native method stackone per threadframes for native (C/C++) methodsStackOverflowError
    Code cacheall threadsmachine code produced by the JIT (Ch 4)JIT stops compiling ("CodeCache is full")

    Metaspace vs PermGen: before Java 8, class metadata lived in a fixed-size heap area called PermGen, famous for OutOfMemoryError: PermGen space. Java 8 replaced it with metaspace, which lives in native memory and grows as needed (cap it with -XX:MaxMetaspaceSize). Metaspace leaks happen when class loaders are created over and over and never freed — e.g. redeploying an app many times inside one server, or generating classes dynamically.

    Under the Hood#

    Stack frames and StackOverflowError, measured#

    Each thread's stack has a fixed maximum size, set with -Xss. Infinite recursion until it overflows:

    Output
    StackOverflowError at depth 47324         (default stack size)
    StackOverflowError at depth 1466          (-Xss256k)
    StackOverflowError at depth 98903         (-Xss4m)

    The depth scales with the stack size. Each frame here was small; frames with many local variables are bigger, so real recursions overflow sooner. This is why a recursive DFS over 100,000 nodes can crash (Operating Systems Ch 6) — and why converting it to a loop with an explicit ArrayDeque stack (Collections, From the Inside Ch 8) or running it on a thread created with a larger stack size fixes it.

    OutOfMemoryError, measured — and a surprise#

    Keep allocating 1 MB arrays with a 64 MB heap:

    Output
    OutOfMemoryError: Java heap space after 30 MB

    Only about 30 MB of our data fitted into a 64 MB heap. Two reasons, both worth knowing: the heap is also used by the JVM's own objects and the collector needs free space to work; and in the G1 collector (Ch 8), an object at least half a region in size is a "humongous" object that gets whole regions to itself — with a 64 MB heap, regions are 1 MB, and a 1 MB array plus its header needs two. Big arrays can waste heap in G1.

    What an object costs — object layout#

    Every object on the heap has a header before its fields: a mark word (8 bytes: hash code, lock state, GC age) and a class pointer (4 bytes with compression). Objects are padded to a multiple of 8 bytes. So:

    Output
      new Integer(5):   12 bytes header + 4 bytes int = 16 bytes           (vs 4 bytes for a plain int)
      new int[0]:       12 bytes header + 4 bytes length = 16 bytes        (an empty array still costs 16)
      a record Cell(int r, int c):  12 + 4 + 4 = 20 → padded to 24 bytes

    That's where the 3 MB vs 21 MB in Classes, Objects and Memory Ch 8 came from. Compressed oops ("ordinary object pointers") store references as 32-bit values instead of 64-bit, halving their size; it's on by default for heaps under ~32 GB — which is one reason people avoid heaps just over 32 GB.

    Check yourself

    5 questions
    1

    Where do objects live? Local variables? Static fields? Method bytecode?

    Show answer

    Heap; the thread's stack frames (references there point to heap objects); on the heap, inside the class's java.lang.Class object; metaspace.

    2

    What replaced PermGen, and where does it live?

    Show answer

    Metaspace, in native memory (outside the Java heap), growing as needed.

    3

    Why did recursion reach depth 98,903 with -Xss4m but only 1,466 with -Xss256k?

    Show answer

    -Xss sets each thread's stack size; a bigger stack holds more frames.

    4

    Why did only ~30 MB of 1 MB arrays fit into a 64 MB G1 heap?

    Show answer

    JVM overhead and GC headroom, plus each 1 MB array was a humongous object occupying two 1 MB regions.

    5

    How big is an Integer object, and why?

    Show answer

    16 bytes: a 12-byte header (mark word + compressed class pointer) plus the 4-byte int value, padded to a multiple of 8.