In short
The Java Virtual Machine is what lets Java code written on one machine run on any other machine that has a JVM. Kamila Oliveira, a Microsoft MVP and backend developer at Ame Digital, breaks the JVM into four parts (the class loader, the memory areas, the execution engine and the native method interface) and then follows a program from source files all the way to native machine code.
Key takeaways
- Explain how Java source becomes bytecode and why that lets one program run on any machine with a JVM.
- Describe the three built-in class loaders and the loading, linking and initialization steps.
- Identify the five JVM memory areas and what each one stores, from the shared heap to per-thread stacks.
- Distinguish the interpreter from the just-in-time compiler and see why hotspots get compiled to native code.
- Choose between the serial, parallel and G1 garbage collectors based on application size and threading.
Why the JVM sits at the core of Java
Oliveira opens by placing the JVM at the center of Java systems and Java-based applications, and of other languages that run on it, such as Clojure, Kotlin and Groovy. Its promise is portability, captured in a familiar slogan: "And to follow the phrase write once, run anywhere, with JVM code writing on one machine can be run on another that uses the JVM." (1:24)
The mechanism is simple at the top level. Java code is compiled into bytecode, an intermediate language that sits between the source code and the host system, and that bytecode is then executed on whatever machine runs the JVM. The rest of the talk opens up the layers that make this possible.
How the class loader brings code into memory
The first layer is the class loader. As Oliveira puts it, "Class loader is responsible for three activities into JVM, loading, linking, and initialization." (2:16) When a Java file is compiled into a .class file, the class loader loads it into main memory. Usually the first class loaded is the one containing the main method, and the JVM records information such as the class name, its parent class, and its modifiers, methods and variables in the method area.
Java ships with three built-in class loaders arranged in a hierarchy. The bootstrap class loader loads the standard Java packages and core libraries. The extension class loader, its subclass, loads extensions to the standard libraries. The application class loader loads files on the class path, which is set to the application's current directory. If a parent cannot find a class, it delegates to its child; if no loader succeeds, the JVM throws a NoClassDefFoundError or ClassNotFoundException.
Linking follows in three steps. Verification checks that the .class file is properly formatted and produced by a valid compiler, and a failure raises a VerifyError. Preparation allocates memory for class variables with default values, and resolution replaces symbolic references. Finally, initialization assigns static variables their values from the code and static blocks, from top to bottom and from parent to child.
Inside the five JVM memory areas
Oliveira next describes JVM memory as five components. The method area stores class-level data such as the runtime constant pool, fields and method code; there is only one per JVM, and it is shared. The heap holds objects and is shared across threads.
Every new thread gets its own runtime stack, which stores local variables, method calls and partial results. "And for each method call, one entry is made in the stack is called stack frame, and when this method call is completed, the stack frame is destroyed." (7:26) Each frame contains local variables, an operand stack for intermediate operations, and frame data including exception-handling information. When a thread needs more stack than is available, the result is a stack overflow error. Each thread also has its own program counter register, which tracks the address of the instruction being executed, and its own native method stack.
Interpreter, JIT compiler and garbage collector
The execution engine runs the bytecode using three parts. "The interpreter that is the first part of execution engine is responsible to read and execute the bytecode line by line." (9:51) That is slow when a method is called repeatedly, because each call is interpreted again.
The just-in-time compiler solves this. When the engine finds repeated code, the JIT compiles it to native code. "Then this native code is used for repeated methods calls. That improves the performance." (10:19) Oliveira lists the JIT's components: an intermediate code generator, a code optimizer, a target code generator and a profiler that finds hotspots, the code executed many times.
The garbage collector removes unreferenced objects from the heap in two steps, mark and sweep. It runs automatically at regular intervals, and developers can also trigger it with System.gc(). Oliveira compares three collectors: "Serial garbage collector is designed for small applications running on single-thread environments. It's the simplest implementation of garbage collector." (12:24) The parallel collector uses multiple threads and, as she describes it, is the default type. G1, or garbage first, is recommended for multithreaded applications with a large heap: it divides the heap into equal-size regions and collects the regions with the most garbage first. She shows the JVM argument that selects each one.
Native methods and the full compile-and-run path
The native method interface acts as a bridge between the JVM and libraries written in languages such as C and C++. "Using the native keyword, you can indicate that the method implementation will be provided by a native library and you need to invoke system load library to load the shared native library into memory and make these functions available to Java." (14:00) Those native libraries usually come as .dll or .so files.
Oliveira closes with an end-to-end example. Three Java source files compile into three corresponding .class files of bytecode, and unlike in C, no linking happens at this stage. At run time the class loader brings the class files into memory, the bytecode is verified for security issues, and the execution engine converts it into native machine code. She notes that this just-in-time step is, in her words, one of the main reasons Java is comparatively slower than other languages.
Watch the talk
"JVM architecture and how it works" by Kamila Oliveira, 16 minutes: https://www.womentech.net/video/jvm-architecture-and-how-it-works
About the speaker
Kamila Oliveira is a Microsoft MVP and Backend Developer at Ame Digital, holds a bachelor's degree in computer science, and co-organizes the WoMakersCode, DevsJavaGirl and PerifaCode communities. She has spoken at conferences including Java Global Summit 2020, Jlove Conference and The Developers Conference, and is a co-author of the book Jornada Java.
Questions this talk answers
- What does the JVM class loader do?
- What happens if no class loader can find a class?
- Why does the JVM use both an interpreter and a JIT compiler?
- How does Java garbage collection work?
- Which garbage collector should I choose?
Drafted with AI from the talk transcript. Editor review pending.