https://www.youtube.com/watch?v=ED1oc7gn5uY

๐ŸŒฑ Big Picture

Starting a simple Java program (like HelloWorld) involves much more than just executing main(). The JVM must first build its entire runtime โ€œuniverseโ€: allocate memory, choose a GC, load core classes, and link them โ€” all before your code runs.


1๏ธโƒฃ Starting the JVM

  • Command: Running java MyApp kicks off JVM startup.
  • Entry point: Internally calls JNI_CreateJavaVM (part of the JNI interface).
  • Tasks at this stage:
    • Parse and validate VM arguments
    • Validate the main class
    • Check system resources (processors, memory, OS utilities)

2๏ธโƒฃ Environment Preparation

  • System resource checks:
    • Number of processors โ†’ influences GC choice
    • Available memory
  • GC selection:
    • Default = G1GC
    • If single CPU or < 1.7 GB memory โ†’ fallback to Serial GC
  • Performance data (HSPerfData):
    • HotSpot-specific
    • Used by tools like jconsole, jvisualvm
    • Stored in temp directory
  • GC roots formed here:
    • The JVM internals (class loaders, runtime tables, monitors).
    • The main thread object (java.lang.Thread) is created in the heap and registered โ†’ pinned as a root.

3๏ธโƒฃ Memory Regions Setup

  • Heap initialisation: Divided into Young (Eden + Survivor spaces) and Old Gen.
  • Metaspace:
    • Stores class metadata (replaced PermGen since Java 8).
    • Allocated from native memory, but still managed by GC.
    • Equivalent to โ€œMethod Areaโ€ in JVM spec.

4๏ธโƒฃ Class Loading (Spec ยง5.3)

  • Performed by the bootstrap class loader (native machine code).
  • Loads essential classes first:
    • java.lang.Object (root of all classes)
    • java.lang.String, java.lang.Class
  • Classes are loaded in tree order (dependencies first).
  • At load time, classes are in a โ€œloaded but not readyโ€ state.
  • GC roots formed here:
    • Static field references of core classes.
    • These remain roots for as long as the class is loaded.
    • Any static variables in your class.

Class Loaders

There are 2 different types of class loaders, the Bootstrap Class Loader and the User-Defined Class Loader

|Feature|Bootstrap Class Loader|User-Defined Class Loader| |---|---|---| |Implemented in|Native code (inside JVM)|Java (extends ClassLoader)| |Loads|Core JDK classes (java.lang.*, java.util.*)|Custom or application-specific classes| |Source|JDK modules / JAVA_HOME/lib|Anywhere: JAR, network, DB, encrypted file| |When used|JVM startup, always first|On-demand by application/framework| |ClassLoader instance|Represented as null in getClassLoader()|Non-null, explicitly created|


5๏ธโƒฃ Linking (Spec ยง5.4)

Three sub-steps:

  1. Verification
    • Ensures the class is structurally correct.
    • May trigger loading of additional classes.
    • JDK classes often skip this step (verified ahead of time via Class Data Sharing).
  2. Preparation
    • Allocate memory for static fields.
    • Initialise them to default values (0, null, etc.).
  3. Resolution
    • Resolve symbolic references in the constant pool โ†’ actual references to methods/fields/classes.
    • Can happen eagerly or lazily (on first use).

6๏ธโƒฃ Initialization (Spec ยง5.5)

  • Executes:
    • Static initializers (static { ... })
    • Assignments to static fields
  • Done via a special synthetic method:
    • <clinit>()V โ†’ automatically generated by the compiler.
  • Distinct from instance initialization (<init> constructor).

7๏ธโƒฃ User Code Execution

  • After ~450 core classes are loaded, linked, and initialized (~62 ms in tests), the JVM finally calls public static void main(String[] args).
  • At this point, the HelloWorld program actually begins.

๐Ÿ”ฎ Future of JVM Startup

  • Project Leyden: Optimise startup and footprint.
  • AOT (Ahead-of-Time) class loading/linking replaces old CDS (Class Data Sharing) terminology.
  • JEP 483: AOT class loading/linking coming in JDK 24.

๐Ÿง  Key Takeaways

  • JVM startup is much more complex than just calling main().
  • Steps include:
    • Argument validation
    • GC selection
    • Metaspace setup
    • Class loading, linking, and initialization
  • Tools like CDS/AOT are being introduced to speed up this process.

 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
 โ”‚                        JVM Startup Flow                       โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

 java MyApp
      โ”‚
      โ–ผ
 JNI_CreateJavaVM (native entry point)
      โ”‚
      โ–ผ
 Parse + validate VM args & main class
      โ”‚
      โ–ผ
 System resource checks (CPU, memory, OS utils)
      โ”‚
      โ”œโ”€โ”€ If โ‰ฅ 2 CPUs & โ‰ฅ 1.7 GB RAM โ†’ G1GC
      โ””โ”€โ”€ Else โ†’ Serial GC
      โ”‚
      โ–ผ
 Setup environment (HSPerfData, monitoring hooks)
      โ”‚
      โ–ผ
 Initialize memory regions
   โ”œโ”€ Heap (Young Gen: Eden + Survivor, Old Gen)
   โ””โ”€ Metaspace (class metadata, native memory)
      โ”‚
      โ–ผ
 Class Loading (bootstrap loader)
   โ”œโ”€ java.lang.Object
   โ”œโ”€ java.lang.String
   โ””โ”€ java.lang.Class โ€ฆ (and dependencies)
      โ”‚
      โ–ผ
 Linking
   โ”œโ”€ Verification (structural checks)
   โ”œโ”€ Preparation (static fields โ†’ default values)
   โ””โ”€ Resolution (constant pool โ†’ real refs)
      โ”‚
      โ–ผ
 Initialization
   โ”œโ”€ Run <clinit>() (static initializers & fields)
   โ””โ”€ Mark class as initialized
      โ”‚
      โ–ผ
 User code starts:
   โ†’ main(String[] args)