Java performance engineering
24 questions in 7 subtopics.
JMH microbenchmarks
What is wrong with this benchmark?long start = System.nanoTime();
for (int i = 0; i < 1_000_000; i++) {
parse("2024-01-01");
}
System.out.println((System.nanoTime() - start) / 1_000_000 + " ns per call");
StringBuilder with String.join for joining ten strings, and explain how you avoid the common pitfalls.A team shows a JMH result where their rewritten hashing routine is 30 percent faster, and wants to replace the routine in a shared library used by 80 services. How do you evaluate the proposal?How would you build a lasting performance engineering culture in a large Java organisation? What roles do microbenchmarks, load tests and production profiling play, and how do you invest in them over years?Allocation pressure
Allocation in a TLAB is just a pointer bump, so why does allocation rate still matter for Java performance?Explain thread-local allocation buffers (TLABs) in HotSpot, and what happens when an object is too large for the current TLAB.JFR shows a service allocating about 2 GB per second, and most of it comes from debug logging:log.debug(String.format("order %s items %s", order.id(), toJson(order.items()))). DEBUG is disabled in production. Explain and fix.Java's performance model has changed over time: escape analysis, compact strings, better collectors, indy-based string concatenation and virtual threads. Which old performance rules of thumb should your organisation retire, and how do you keep guidance current?False sharing
What is a CPU cache line, and what is false sharing?Two threads each increment their own counter inclass Counters {
volatile long a;
volatile long b;
}
Boxing costs
Why is this loop slow, and what else could go wrong with boxed types?Long sum = 0L;
for (long i = 0; i < n; i++) {
sum += i;
}
if (order.getQuantity() == other.getQuantity()) where both getters return Integer. Tests pass with small quantities. What is the bug?A service keeps a Map<Long, Long> with 50 million entries and it uses roughly 4 GB of heap. Estimate where the memory goes and propose alternatives.Collection choice and performance
A worker queue holds up to one million pending tasks; producers add at the end and consumers remove from the front. A developer usedArrayList with remove(0). What is the problem, and which collection should be used?A profile of a batch job shows significant time in HashMap.resize. The code creates new HashMap<>(expectedSize) and then inserts exactly expectedSize entries. Why does it still resize, and how do you fix it?You need an in-memory structure of price updates keyed by timestamp that supports range queries (all updates between two times) and is read and written by many threads. Which Java collection would you choose and why?Latency vs throughput
Explain latency versus throughput, why average latency is misleading, and how you should measure and report latency for a service.One deployment of a service handles both interactive API traffic with a 100 ms p99 target and large nightly batch imports. The batch team wants Parallel GC and large thread pools, the API team wants low pauses. How do you resolve this?A checkout flow crosses 12 services and has an end-to-end p99 target of 800 ms. Set up performance budgets: how are they split, who owns them, and how are they enforced?At company level, how do you decide when to invest engineering time in performance work versus buying more hardware, and how do you keep that decision sound over years?Mechanical sympathy
What does mechanical sympathy mean for a Java developer? Give two examples where knowing the hardware changes how you write code.Summing an int[4096][4096] row by row takes 20 ms, but summing it column by column takes over 200 ms. Explain why.You are designing a low-latency order matching component in Java with a target of tens of microseconds per order. Which mechanical sympathy principles would guide the design?Server hardware keeps changing: more cores per socket, ARM-based instances with different price and performance, large caches, and memory bandwidth limits. How should these trends shape your company's Java performance strategy over the next five years?No question matches these choices.