You compile .java to .class bytecode. The Java Virtual Machine loads classes, verifies bytecode, interprets or JIT-compiles to native code, allocates objects, and runs GC. “Java” the language and “the JVM” are not the same — other languages also target the JVM.
LTS you should name: Java 17 and Java 21 are the current long-term professional baselines. Features in this hub assume 17 unless labelled 21.
javac → bytecode. java launches a JVM. jar is a zip of classes plus a manifest.| Area | Holds | Failure mode |
|---|---|---|
| Stack (per thread) | Frames: locals, return address | StackOverflowError (unbounded recursion) |
| Heap | Objects, arrays | OutOfMemoryError: Java heap space |
| Metaspace | Class metadata | OutOfMemoryError: Metaspace (classloader leaks) |
| Native | Direct buffers, JNI, thread stacks | Native OOM; Direct buffer memory |
Person is a reference in the frame; the Person is on the heap.-Xmx / -Xms (heap), -XX:MaxMetaspaceSize, -Xss (thread stack). Do not cargo-cult twenty GC flags.Direct memory: ByteBuffer.allocateDirect and many NIO/Netty paths. They are not in -Xmx. Leaking them looks like native OOM while heap looks fine.
GC finds objects that are not reachable from GC roots (stacks, static fields, JNI handles) and reclaims them. Generational collectors assume most objects die young.
Collectors (know the names): G1 is the default on modern LTS HotSpot (good general choice). ZGC and Shenandoah target low pauses (available on 17/21; defaults and readiness depend on vendor). Parallel GC is throughput-oriented. Serial is for small heaps/tools. Do not switch collector because of a blog post; switch because of a pause/throughput measurement.
Reachability: a static Map that grows is a leak. A cache without eviction is a leak. A listener list that never removes is a leak. finalize is not a cleanup strategy (deprecated for removal).
// Classic leak — static collection
static final List<byte[]> CACHE = new ArrayList<>();
void handle(byte[] body) { CACHE.add(body); } // never removed
Tools: heap dump (jmap, jcmd GC.heap_dump), jcmd GC.heap_info, GC logs (-Xlog:gc* on 17+), async-profiler / JFR. Read a dump in Eclipse MAT or VisualVM when you have a leak, not before.
A class is uniquely identified by its name plus its classloader. The same bytecode loaded twice in two loaders is two classes. That is the source of mysterious ClassCastException / LinkageError in app servers and plugin systems.
ExceptionInInitializerError.Thread.currentThread().getContextClassLoader() is how libraries load resources in containers. If you spawn threads, inherit or set it deliberately.ServiceLoader / META-INF/services (and module provides) is the JDK plugin mechanism. Reflection (Class.forName, getMethod, invoke) bypasses compile-time checks — use it at boundaries, not as your architecture.
| Classpath | JPMS modules (Java 9+) | |
|---|---|---|
| Unit | JARs / directories of classes | module with module-info.java |
| Visibility | Everything public is visible | Only exported packages are visible to other modules |
| Split packages | Last jar wins (silent pain) | Forbidden |
| Typical app | Still the majority of Spring/Maven apps | The JDK itself; some libraries |
java -cp app.jar:lib/* com.acme.Main (use ; on Windows).IllegalAccessError, --add-opens (a smell you should track), and why reflective frameworks ask for it.Resources: getClass().getResourceAsStream("/config.yml") vs module-oriented ClassLoader.getSystemResource. Leading slash rules differ between Class and ClassLoader — know which API you called.
OutOfMemoryError is thrown on the allocating thread; others may keep running. Treat the JVM as dying: capture the dump (HeapDumpOnOutOfMemoryError) and restart.--add-opens is a debt list.-Xmx from folklore.~ Good luck with your revision! ~