skip to content

Optimization Techniques

Code-level techniques that actually pay off — avoiding quadratic string building, pre-sizing collections, dodging autoboxing — alongside the JIT behaviors that make other optimizations unnecessary. Interviewers want to see you distinguish the two.

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

explore

questions

page 2 of 2

You profile a service and find a hot method spending most of its time in String operations while building a large CSV-like payload in a loop. How do you diagnose and fix it, and what trade-offs guide your choice of approach?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Look for += String building inside the loop — that's almost certainly the culprit, because it copies everything each turn (O(n^2)). Replace it with a StringBuilder (append in the loop, toString once), pre-size it if you can estimate the length, and re-profile to confirm the time and garbage dropped.

open as a page

As a system designer, how do you decide between eager and lazy initialization given startup time versus first-access latency, and what techniques mitigate the downsides?

level: principalimportance: should knowfreq 40%

basics

~20 s

Eager init is slower to start but every request is fast and predictable; lazy init starts faster but the first user of each lazy thing pays a latency spike. Choose eager for things always needed or latency-sensitive, lazy for rarely-used expensive things, and warm up critical paths to hide the spike.

open as a page

How do you weigh the readability and maintainability cost of an optimization against its measured benefit, and how do you decide it's worth it?

level: principalimportance: should knowfreq 50%

basics

~20 s

Every optimization that makes code harder to read or change has a cost you pay forever. Only accept it when measurement shows a meaningful, needed gain on a real hotspot — and document why, so future readers don't "simplify" it back.

open as a page

As a senior engineer, how would you decide whether autoboxing is actually worth optimizing in a given codebase, and what's the future direction (Project Valhalla)?

level: seniorimportance: nice to knowfreq 34%

basics

~20 s

Don't guess — profile. Check allocation rate and GC time; if boxed wrappers dominate, optimize the hot path with primitives or primitive collections. For cold or rarely-run code, leave the readable boxed version. Project Valhalla aims to let value/primitive classes remove much of this cost in the future.

open as a page

When you build collections via the Streams API (collect, toList, groupingBy), can you still benefit from pre-sizing, and what are the limits of doing so?

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

Streams usually can't pre-size their result because the element count isn't known until the stream finishes — collectors append into a default-capacity collection that grows as usual. If you know the size and it's a hot path, build the collection manually with a pre-sized constructor, or use a collector supplier that creates a pre-sized container.

open as a page

What is lock elision (biased toward synchronization removal) via escape analysis, and when can the JIT safely drop a synchronized block?

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

Lock elision is when the JIT removes the locking from a synchronized block because escape analysis proves the lock object can only ever be touched by one thread. If no other thread can see the object, the lock protects nothing, so it is safe to delete.

open as a page

Given that branch prediction, loop unrolling, bounds-check elimination and prefetching are largely handled by the JIT and the hardware, when (if ever) should a Java engineer deliberately code for these CPU-level effects, and how should they decide?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Almost never by default — write simple, predictable loops and let the JIT and CPU optimize. Only after a profiler proves a specific hot loop is the bottleneck, and only if simpler fixes (better algorithm or data structure) don't suffice, should you reshape data, go branchless, or use the Vector API — then re-measure to confirm it actually helped.

open as a page

Why is escape analysis fragile? Identify concrete code patterns that defeat it, and how inlining limits affect it.

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Escape analysis only optimizes objects it can prove never leave a method. Returning the object, storing it in a field, or passing it to a method the JIT can't see into all make it escape. It also depends on inlining, so anything that prevents inlining indirectly defeats it.

open as a page

As a tech lead, how would you decide whether to introduce object pooling on a hot path, and what evidence would you require?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

First measure: profile allocation and GC to confirm object creation is actually a problem. Only pool if the object is expensive to make or scarce, like threads, connections, or big buffers. For ordinary objects, prefer letting the GC work. If you do pool, use a proven library, not hand-rolled code.

open as a page

showing 31–39 of 39