Java Materials
← Core Java · Chapter 9

JVM internals, memory and diagnostics

Class loading finds a definition, linking verifies and prepares it, and initialization runs static initializers before active use. Parent delegation avoids duplicate core types in conventional loaders. Class identity includes the defining loader. Two types with identical names can therefore be incompatible.

Class identity, loaders and leaks

Definition:

JVM class identity combines the binary name with the defining class loader.

Explanation:

Class identity is the pair of name and defining loader.

Illustration image:

Class identity, loaders and leaks. Loader A defines Api · Loader B defines Api · Same binary name · Different class identitiesClass identity, loaders and leaksSource or actorLoader A defines ApiFirst boundaryLoader B defines ApiInspected stateSame binary nameObservable effectDifferent class identities
Loader A defines Api · Loader B defines Api · Same binary name · Different class identities

Example:

Worked Java example · see surrounding context in notes
System.out.println(plugin.getClass().getName());
System.out.println(plugin.getClass().getClassLoader());
System.out.println(Api.class.getClassLoader());

Expected behavior. Compare defining loaders when identical names still fail an interface cast.

Internal working:

A plugin can fail a cast even when names match if private copies are loaded by different loaders. Thread context loaders, global registries and application-created threads can keep an old deployment alive. Establish shared API loading and explicitly unregister resources during unload.

Interview questions:

Why does a class with the right name still fail a cast?

Two class loaders may have defined independent classes with that binary name.

GC, heap retention and native memory

Definition:

Garbage collection uses reachability to determine which managed objects can be reclaimed.

Explanation:

A collector reclaims unreachable storage; increasing heap size does not fix objects still retained by a cache, task queue or ThreadLocal.

Illustration image:

GC, heap retention and native memory. GC root · Live object graph · Unreachable objects · Native memory is separateGC, heap retention and native memorySource or actorGC rootFirst boundaryLive object graphInspected stateUnreachable objectsObservable effectNative memory is separate
GC root · Live object graph · Unreachable objects · Native memory is separate

Example:

Shell / diagnostic commands
jcmd PID GC.class_histogram
jcmd PID JFR.start name=allocations duration=60s filename=allocations.jfr

Expected behavior. A histogram shows class populations; a recording helps explain where allocations and hot paths originate.

Internal working:

Distinguish allocation churn from post-GC retention. Direct buffers, metaspace and stacks are outside ordinary Java heap measurements. Compare live set, pauses, allocation and process memory before selecting collector changes.

Interview questions:

Why might process memory grow while the heap live set stays flat?

Native buffers, thread stacks, metaspace or other native allocations may explain the growth; inspect them separately.

JIT, warm-up and measuring latency

Definition:

The JIT compiles frequently executed bytecode based on runtime observations.

Explanation:

HotSpot may inline and optimize based on observed call shapes.

Illustration image:

JIT, warm-up and measuring latency. Cold interpreter · Profile gathered · Hot compiled code · Potential deoptimizationJIT, warm-up and measuring latencySource or actorCold interpreterFirst boundaryProfile gatheredInspected stateHot compiled codeObservable effectPotential deoptimization
Cold interpreter · Profile gathered · Hot compiled code · Potential deoptimization

Example:

Shell / diagnostic commands
java -Xlog:jit+compilation=debug -jar app.jar

Expected behavior. On supported HotSpot releases, inspect compilation activity during a targeted run; do not use verbose logs indiscriminately in production.

Internal working:

If assumptions stop holding it can deoptimize and recompile. A microbenchmark that discards results may measure eliminated code, and a short test may measure startup rather than steady state. Use JMH with forks and warm-up; for service latency also vary warm and cold scenarios.

Interview questions:

Why are short timings of a cold Java method misleading?

They can include interpretation, class initialization and JIT warm-up rather than steady-state work.

Reference subtopics

Memory and execution

Thread stacks contain frames for locals and operands. The heap holds objects, metaspace stores HotSpot class metadata, and direct buffers and thread stacks consume native memory. Interpretation starts execution and JIT compilation optimizes hot code. Inlining and escape analysis can remove call or allocation work; warm-up changes benchmark results.

Reachability and garbage collection

GC starts from roots such as live stacks and static references. Strong references maintain reachability. Soft references can be cleared at collector discretion, weak references clear when weakly reachable and phantom references support post-mortem notification with a queue. Monitor allocation rate, live set, pauses and CPU overhead together.

Collectors and failure classes

Parallel GC prioritizes throughput. G1 balances regional collection with pause targets. ZGC minimizes pauses with different CPU and memory trade-offs; available variants depend on release. A StackOverflowError usually indicates excessive stack depth. OutOfMemoryError can concern heap, native allocation, direct buffers, metaspace or thread creation.

Leak and incident evidence

Compare retained objects, class loaders, caches, listeners and ThreadLocal lifetimes. A heap dump exposes retention but may affect production or contain confidential data. Use thread dumps for locks, GC logs for allocation and pauses, and JFR with Java Mission Control for time-series CPU and allocation evidence. Always compare before and after under matched load.

Run the complete Java labs ↗ · Java reference ↗