PrepPilotPractise free

JVM Internals & Performance interview questions

Memory, GC, class loading, profiling and tuning

17 questions · -Xmx · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is the difference between heap and stack memory in the JVM?

Heap memory is used for storing objects and is managed by garbage collection, while stack memory holds method call frames and local variables, and is managed per thread. Stack memory is faster but limited in size, whereas heap is larger and shared across threads.

What is the purpose of the JVM metaspace?

Metaspace stores class metadata, such as class definitions and related structures. Unlike the old PermGen, metaspace is allocated from native memory and grows as needed, reducing the risk of class metadata exhaustion.

How does generational garbage collection improve performance?

Generational GC divides heap into Young and Old generations, optimizing for short-lived objects by collecting them frequently in the Young generation. This reduces the overhead of scanning the entire heap, improving GC efficiency and application performance.

What tool would you use to take a heap dump of a running JVM process?

I would use jcmd <pid> GC.heap_dump <file> or jmap -dump:format=b,file=<file> <pid> to capture a heap dump for analysis.

Intermediate questions

Explain how G1 garbage collector works and when you would use it.

G1 GC divides the heap into regions and prioritizes collecting regions with the most garbage. It performs concurrent marking and evacuation to minimize pause times. I use G1 GC for applications requiring predictable pause times and moderate throughput, especially in large heap scenarios.

How would you diagnose a Java heap OutOfMemoryError in production?

I would enable heap dumps with -XX:+HeapDumpOnOutOfMemoryError, collect the dump, and analyze it using tools like Eclipse MAT to find memory leaks or large objects. I also check JVM logs and use jcmd or jmap for real-time analysis.

What is a thread dump and how can it help diagnose high CPU usage?

A thread dump shows all threads and their stack traces. By analyzing thread dumps with jstack, I can identify threads stuck in loops or locks, which often cause high CPU. Tools like async-profiler help visualize CPU hotspots and pinpoint problematic code.

How does JIT compilation affect Java application performance?

JIT compilation converts frequently executed bytecode into optimized native code at runtime, improving performance. However, initial execution may be slower until the JVM identifies hotspots and applies optimizations, so proper warm-up is important for accurate profiling.

Describe a common classpath problem and how you would resolve it.

A common issue is ClassNotFoundException due to missing or misconfigured JARs. I check the classpath setup, verify dependencies, and ensure there are no conflicting versions. Tools like jdeps and examining the class loader hierarchy help resolve these issues.

How do JVM flags like -Xmx and -XX:MaxRAMPercentage affect memory allocation?

-Xmx sets the maximum heap size, while -XX:MaxRAMPercentage configures heap as a percentage of available RAM, which is useful in containers. These flags help control memory usage and prevent the JVM from exceeding system or container limits.

What is the role of -XX:+HeapDumpOnOutOfMemoryError in production?

This flag instructs the JVM to generate a heap dump automatically when an OutOfMemoryError occurs. It is crucial for post-mortem analysis, allowing engineers to investigate memory leaks or excessive object retention after a failure.

How can you monitor JVM memory and GC activity in production?

I use JVM flags like -Xlog:gc* or -verbose:gc for logging, and tools like JMX, Prometheus exporters, or Grafana dashboards to track heap usage, GC pauses, and allocation rates in real time.

Senior questions

How would you tune JVM garbage collection for a latency-sensitive microservice running in a Kubernetes pod?

See the answer in the app, free

Describe your approach to diagnosing high CPU usage in a Java application running in production.

See the answer in the app, free

How would you handle a Metaspace OutOfMemoryError in a large application?

See the answer in the app, free

Explain the impact of JVM container awareness on resource allocation and performance.

See the answer in the app, free

What are the trade-offs between G1, ZGC, and Parallel GC for a large-scale Java application?

See the answer in the app, free

More Java topics