skip to content

Performance & Optimization

Measuring and improving Java performance: benchmarking with JMH, profiling, code-level and JIT-aware optimizations, allocation behavior, and the JDK diagnostic tools. Interviewers use this area to test discipline — measure first — as much as knowledge of tricks.

part ofJavaoverview, primer and where to startread it →
on this pageshow

explore

questions

94 · 3 sections

What is JMH, and why should you use it instead of hand-rolled timing with System.nanoTime() loops for Java microbenchmarks?

level: juniorimportance: must knowfreq 55%
basics
~20 s

JMH is the Java Microbenchmark Harness, an official tool for measuring how fast small pieces of Java code run. It's better than manual nanoTime loops because the JVM warms up and optimizes code over time, and JMH handles that warmup and the JIT effects so your numbers are trustworthy.

open as a page

What is dead-code elimination in the context of a JMH benchmark, and why can it make a microbenchmark report a misleadingly fast result?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Dead-code elimination is when the JVM deletes code whose result is never used. In a benchmark, if you compute something but never use the value, the JIT may delete the work, so the benchmark times nothing and reports an impossibly fast result.

open as a page

What is the difference between @Warmup and @Measurement iterations in JMH, and how do you configure their count and time?

level: juniorimportance: must knowfreq 60%
basics
~20 s

@Warmup runs the code to get the JVM up to speed and throws those numbers away. @Measurement runs it again and keeps those numbers. You set how many rounds (iterations) and how long each round lasts (time).

open as a page

Why must a Java microbenchmark include a warmup phase, and what happens if you measure the very first iterations of a hot loop?

level: juniorimportance: must knowfreq 70%
basics
~20 s

The JVM starts running code interpreted and only compiles hot code to fast machine code after it runs many times. If you measure the first runs, you measure slow startup, not the real speed. Warmup runs the code first so the fast version is in place before timing.

open as a page

What is the difference between a sampling profiler and an instrumentation (tracing) profiler in Java?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A sampling profiler peeks at what the program is doing every so often and adds up where it spends most time. An instrumentation profiler adds extra code to every method to count and time every single call exactly, which is more accurate but much slower.

open as a page

What are autoboxing and unboxing in Java, and why can they hurt performance in hot code?

level: juniorimportance: must knowfreq 72%
basics
~20 s

Autoboxing converts a primitive (like int) into its wrapper object (Integer) automatically; unboxing does the reverse. Each box can allocate a new object, which costs memory and creates garbage. In tight loops that adds up and slows things down.

open as a page

What is lazy initialization in Java, and when would you use it?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Lazy initialization means you delay creating an object until the first time it is actually needed, instead of creating it up front. You use it when the object is expensive to build and might never be used.

open as a page

What does "premature optimization is the root of all evil" mean, and what is the correct discipline it prescribes?

level: juniorimportance: must knowfreq 70%
basics
~20 s

It means don't make code faster before you know it's slow. First write clear, correct code, then measure where the real slowness is, and only optimize that part. Guessing usually wastes effort and hurts readability.

open as a page

Why is building a String by repeatedly using += inside a loop slow, and how slow does it get as the number of iterations grows?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Strings in Java can't be changed, so each += makes a brand-new String by copying all the characters so far plus the new ones. Doing that every loop turn means you copy more and more text, so the total work grows much faster than the number of loops.

open as a page

Explain the Integer cache (-128..127) and why `==` on boxed Integers is dangerous.

level: middleimportance: must knowfreq 80%
basics
~20 s

Integer.valueOf caches small Integer objects from -128 to 127 and reuses them. So two boxed 100s are the same object (== is true), but two boxed 1000s are different objects (== is false). Use .equals() to compare values, not ==, which compares object identity.

open as a page

What is jcmd, and why is it considered the modern, recommended replacement for one-off tools like jmap and jstack?

level: juniorimportance: must knowfreq 70%
basics
~20 s

jcmd is a JDK command-line tool that sends diagnostic commands to a running JVM. Instead of separate tools, one tool can dump the heap, print threads, run GC, show flags, and more. It's safer and unified, so it replaces jmap and jstack.

open as a page

What are jconsole and VisualVM, and when would you reach for each to monitor a running JVM?

level: juniorimportance: must knowfreq 60%
basics
~20 s

Both are free GUI tools that ship with (or pair with) the JDK to watch a live Java program: heap memory, garbage collection, thread count and CPU. jconsole is the minimal built-in monitor; VisualVM does more, including CPU/memory profiling and dumps.

open as a page

What is jmap, and what are its two most common uses for inspecting a running Java application's heap?

level: juniorimportance: must knowfreq 62%
basics
~20 s

jmap is a JDK command-line tool that inspects a running Java program's heap (its object memory). Its two main uses are printing a histogram of how many objects of each class exist, and dumping the whole heap to a file for offline analysis.

open as a page

What is jstack and what does it produce? When would you reach for it?

level: juniorimportance: must knowfreq 68%
basics
~20 s

jstack is a JDK command-line tool that prints a thread dump: a snapshot of every thread in a running Java process and what line of code each one is currently executing. You use it when an app hangs, is frozen, or is using too much CPU.

open as a page

How do you capture a thread dump with jcmd, and what would you look for in the output when diagnosing a hang or high CPU?

level: middleimportance: must knowfreq 68%
basics
~10 s

Run 'jcmd <pid> Thread.print' to print every thread's stack. To diagnose a hang look for BLOCKED threads and deadlocks; for high CPU look for RUNNABLE threads stuck in the same code across several dumps.

open as a page