Skip to content

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

    Java / Inside the JVM

    The JVM's Parts

    The JVM specification defines what a JVM must do; HotSpot (in OpenJDK and Oracle's JDK) is the implementation almost everyone runs, and it's what

    Chapter 1 of 103 min read

    Start With the Problem#

    StudyBuddy's Java API service has three production mysteries:

    1. Most requests take 20 ms, but every few minutes, a burst of requests takes 800 ms. CPU is fine. The database is fine.

    2. Memory on the monitoring graph goes up, up, up, then suddenly drops — a sawtooth — and one day it stopped dropping and the service died with java.lang.OutOfMemoryError: Java heap space.

    3. After deploying, the service is slow for the first minute, then fast.

    All three are explained by what's inside the JVM: the garbage collector pausing the application (1), the heap filling with garbage and being cleaned — until something kept objects alive that shouldn't be (2), and the JIT compiler warming up (3). This file opens the JVM up.

    The Plain Picture#

    A theatre production has three departments: the stage crew who bring in scenery and props when a scene needs them, the stage and backstage space where everything is kept during the show, and the actors and cleaners who perform the script and keep the stage clear.

    The Real Names#

         .class files (bytecode)
                │
                ▼
      ┌───────────────────────────────┐
      │  1. CLASS LOADER SUBSYSTEM    │  load → link → initialise (Ch 2)
      └──────────────┬────────────────┘
                     ▼
      ┌──────────────────────────────────────────────────────────────────┐
      │  2. RUNTIME DATA AREAS (Ch 3)                                    │
      │   shared by all threads:    HEAP (objects)   METASPACE (classes) │
      │   one per thread:           JVM STACK   PC REGISTER   NATIVE STACK│
      │   plus:                     CODE CACHE (JIT-compiled machine code)│
      └──────────────┬───────────────────────────────────────────────────┘
                     ▼
      ┌───────────────────────────────┐        ┌────────────────────────┐
      │  3. EXECUTION ENGINE          │ ◀────▶ │ native libraries (JNI) │
      │   interpreter · JIT (Ch 4)    │        └────────────────────────┘
      │   garbage collector (Ch 5–8)  │
      └───────────────────────────────┘
    • The JVM specification defines what a JVM must do; HotSpot (in OpenJDK and Oracle's JDK) is the implementation almost everyone runs, and it's what this file describes.

    • JNI (Java Native Interface) lets Java call code written in C/C++.

    Check yourself

    2 questions
    1

    Name the three main parts of the JVM.

    Show answer

    Class loader subsystem, runtime data areas, execution engine.

    2

    Which memory areas are shared by all threads, and which are per thread?

    Show answer

    Shared: heap, metaspace (and code cache). Per thread: JVM stack, PC register, native method stack.