Java Is Fast. Your Code Might Not Be.
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
A Java order-processing demo app was optimized from 1,198ms to 239ms (5x throughput, 87% less heap, 79% fewer GC pauses) by fixing eight common anti-patterns. The patterns covered are: string concatenation in loops causing O(n²) copies, accidental O(n²) stream iteration inside loops, String.format() in hot paths, autoboxing creating millions of wrapper objects, exceptions used for control flow triggering expensive stack trace generation, overly broad synchronization creating lock contention, repeated creation of reusable objects like ObjectMapper, and virtual thread pinning on JDK 21–23. Each pattern includes a concrete before/after code fix and explanation of the underlying JVM cost. The post emphasizes that none of these patterns cause failures—they silently drain CPU and inflate heap, and only become visible through profiling tools like Java Flight Recorder.
Table of contents
1. String Concatenation in Loops #2. Accidental O(n²) with Streams Inside Loops #3. String.format() in Hot Paths #4. Autoboxing in Hot Paths #5. Exceptions for Control Flow #6. Too-Broad Synchronization #7. Repeated Creation of “Reusable” Objects #8. Virtual Thread Pinning (If You’re on JDK 21–23) #The Compounding Effect #Sort: