JVM
The Java Virtual Machine is the program that actually runs your compiled Java code. It loads class files, checks them for safety, executes them, and manages memory for you.
Detailed Explanation
Your CPU does not understand Java. It understands machine instructions for one specific processor family. The JVM sits between the two. javac compiles your source to a portable instruction set called bytecode, and the JVM turns that bytecode into real machine work on whatever machine it is installed on.
That indirection is the whole point. Before portable runtimes were common, shipping a desktop application meant compiling it separately for each operating system and processor, and maintaining those builds forever. A .class or .jar file produced on a laptop runs unchanged on a Linux server, because the platform-specific part was moved out of your build and into the JVM installation.
The JVM is a specification first and a program second. HotSpot, shipped with most JDK builds, is the common implementation, but OpenJ9 and GraalVM Native Image also exist. Anything the specification guarantees, such as the size of an int or the ordering rules of the Java memory model, holds on every implementation.
Execution happens in stages. A class loader reads a class file when it is first needed, not at startup. The verifier then checks the bytecode: that the stack does not underflow, that types line up, that you cannot forge a reference. This is why a corrupt class file throws VerifyError instead of crashing the process. Linking resolves references to other classes, and static initializers run on first active use.
Then the interpreter starts executing bytecode one instruction at a time. That is slow, so HotSpot counts how often each method and loop runs. Once a method crosses a threshold, the just-in-time compiler translates it to native code, optimising with information an ahead-of-time compiler never has: which branch actually gets taken, which types actually arrive, which calls can be inlined. Tiered compilation means hot code gets compiled twice, quickly by C1 and then aggressively by C2.
The JVM also owns memory. The heap holds objects, garbage collection reclaims them, each thread gets its own stack, and class metadata lives in Metaspace. Java 8 replaced the fixed-size PermGen with Metaspace, which is why OutOfMemoryError: PermGen space is a pre-8 message.
You reach for JVM knowledge when behaviour stops making sense: a benchmark that is slow for the first thousand iterations and fast afterwards, a latency spike with no matching code path, a ClassNotFoundException at runtime for a class that compiled fine.
The costs are real. Startup is slower than a native binary because classes load and verify lazily and hot code must warm up. Baseline memory footprint is higher. GC pauses, though very short on modern collectors like G1 (default since JDK 9) and generational ZGC (JDK 21), are still pauses. In exchange you get portability, memory safety, and a runtime that optimises code you did not hand-tune.
Code Examples
public class JvmInfo {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
// Which JVM implementation is executing this code right now.
System.out.println(System.getProperty("java.vm.name"));
// prints: OpenJDK 64-Bit Server VM
// The JVM decides these limits, not your operating system directly.
System.out.println("max heap MB: " + runtime.maxMemory() / (1024 * 1024));
System.out.println("cpus: " + runtime.availableProcessors());
}
}public class Warmup {
// The JIT compiler needs to see this run many times before it optimises it.
static long checksum(byte[] data) {
long total = 0;
for (byte b : data) {
total += b & 0xFF; // mask to 0..255 because byte is signed in Java
}
return total;
}
public static void main(String[] args) {
byte[] payload = new byte[1_000_000];
for (int round = 0; round < 5; round++) {
long start = System.nanoTime();
checksum(payload);
long micros = (System.nanoTime() - start) / 1_000;
System.out.println("round " + round + ": " + micros + " us");
}
// Later rounds are usually several times faster than round 0.
// Nothing about the code changed; the JIT recompiled it to native code.
}
}Common Gotchas
Timing the first run measures the interpreter
a "benchmark" that calls a method once is measuring bytecode interpretation and class loading, not your algorithm. Use JMH, or at minimum discard several warmup rounds, before quoting a number.
Compiled against one version, running on another
the JVM refuses to load class files newer than itself and throws `UnsupportedClassVersionError`. Building on JDK 21 and deploying onto a JDK 17 runtime fails at startup. Set `--release` in your build to target the runtime you actually deploy to.
A JVM crash is not the same as an exception
`OutOfMemoryError` and `StackOverflowError` come from the JVM running out of a resource, not from your logic being wrong in the usual sense. Catching them rarely helps, because the process is already in a degraded state.