Skip to content
Tutorials Center
My learning

Topics

Lesson 164 of 201 ยท JVM and Advanced Topics

Java Performance Tips

Make Java code faster the right way - measure with nanoTime and JMH, avoid boxing and string concatenation in loops, pick the right collection, cache and profile.

Java is fast. The JVM's just-in-time (JIT) compiler turns hot code into optimized machine code at runtime, and for long-running server applications Java competes with C++ in many workloads. Yet slow Java programs are everywhere, and almost always for the same few reasons: the wrong algorithm or collection, needless object creation, repeated work that could be done once, or slow I/O. Clever micro-tricks rarely matter; those big mistakes do.

This lesson teaches the professional approach: measure first, understand why naive timing lies, and then fix the problems that really cost time: boxing, string building, collection choice, repeated work and I/O. You'll also see what the JIT does for you (so you stop "optimizing" things it already handles) and which profiling tools find the real hot spots in a big application. It builds on Memory and Garbage Collection and JVM Architecture.

Rule one: measure, don't guess

Developers are famously bad at guessing where time goes. A method that looks slow may run in microseconds, while an innocent-looking list.contains inside a loop eats 90% of the time. So the workflow is always:

  1. Decide what "fast enough" means (a response time, a batch duration).
  2. Measure the real program to find where time is spent (profile it).
  3. Change one thing.
  4. Measure again, and keep the change only if it helps.

The simplest measuring tool is System.nanoTime(), which returns a high-resolution timestamp meant for measuring durations. Use it, not System.currentTimeMillis(), which is wall-clock time: it can jump when the system clock is adjusted, and it's often only accurate to a few milliseconds.

There's a catch, though. The JVM starts by interpreting bytecode, then compiles methods that run often. The first calls of a method are therefore slower than later ones, and a single measurement mostly measures this warm-up:

Java Warm-up: the same work gets faster

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The first round (sometimes the first two) is clearly slower than the rest: it ran while the code was still being interpreted and compiled. Run it a few times and you'll also see the numbers wobble from run to run. Any single timing is noisy.

About timings on this site. Our sandbox runs each program in a small JVM tuned for quick startup: one CPU core, a small heap and only the first, fast tier of the JIT compiler. Absolute numbers will differ from your machine, often by a lot, and the warm-up effect is smaller than on a full JVM. The ratios between good and bad versions in this lesson are what matter, and they hold everywhere.

Benchmarking without fooling yourself

A micro-benchmark times a small piece of code. Writing one that tells the truth is surprisingly hard, because the JVM is smart:

  • Warm-up. Run the code many times before measuring, so you time compiled code (unless startup time is what you care about).
  • Repeat and use the median. Garbage collection, other processes and CPU frequency changes add noise. Take several measurements and report the median, not one lucky (or unlucky) run.
  • Use the result. If a computed value is never used, the optimizing JIT is allowed to skip computing it, and your "benchmark" measures an empty loop. Always consume results, for example by adding them to a field you print at the end.
  • Measure realistic data. Sorting an already sorted list, or looking up the same key a million times, measures the CPU cache, not your code.

The examples in this lesson use a tiny helper that follows those rules: three warm-up runs, five measured runs, the median, and every result added to a sink field. It's good enough to compare two versions of a loop. For serious measurements, Java has a dedicated tool: JMH, the Java Microbenchmark Harness, written by the JDK's own performance engineers. It handles warm-up, forks fresh JVMs, prevents dead-code elimination and reports error margins:

Java
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(2)
public class JoinBenchmark {
    @Param({"10", "1000"})
    int size;

    @Benchmark
    public void plusInLoop(Blackhole blackhole) {
        String s = "";
        for (int i = 0; i < size; i++) {
            s += i;
        }
        blackhole.consume(s);   // stops the JIT from discarding the work
    }

    @Benchmark
    public String builder() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(i);
        }
        return sb.toString();   // returned values are consumed automatically
    }
}

JMH isn't available in our sandbox (it runs benchmarks for minutes, in separate JVMs), so this is read-only. You add it to a project with your build tool and run the benchmarks from the command line.

Boxing costs

A long is 8 bytes on the stack. A Long is an object on the heap with a header and a reference to it. Autoboxing converts between them silently, which makes it easy to create millions of objects without noticing. A classic accident is a Long accumulator, where total += i unboxes, adds and boxes a new Long on every iteration:

Java Primitives vs boxed numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The boxed accumulator is many times slower than the primitive one, and summing a List<Integer> is slower than summing an int[]: each element is a separate object that must be found in memory and unboxed. What to do about it:

  • Use primitive types for counters, sums and loop variables. Watch for Long, Integer and Double in method signatures that don't need null.
  • For number crunching, use arrays or primitive streams (IntStream, mapToInt(...).sum()) instead of collections of wrappers. Libraries such as Eclipse Collections and fastutil offer primitive collections (IntList, LongSet) when you need them.
  • Remember that boxed values in a HashMap<Integer, Integer> are fine for moderate sizes; this matters in hot loops over millions of elements.

Building strings

Strings are immutable, so s += x creates a brand-new string and copies all the old characters into it. In a loop that's quadratic: the 10,000th append copies about 10,000 characters. StringBuilder appends into a growable buffer instead:

Java String concatenation in a loop vs StringBuilder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The gap grows with the number of appends, so code that's fine with 50 items can collapse with 50,000. Note what this does not mean: a single expression like "Order " + id + " for " + name is perfectly fine. Since Java 9 the compiler turns it into one efficient operation (via StringConcatFactory). Only repeated += on the same string in a loop is the problem.

Related string tips: String.format and formatted parse their format string on every call, so avoid them in very hot loops; String.join and Collectors.joining are clean and efficient for lists; and comparing with equals is cheap, but toLowerCase().equals(...) creates a new string each time, so prefer equalsIgnoreCase.

Choosing the right collection

No micro-optimization beats a better data structure. ArrayList.contains checks elements one by one (O(n)), while HashSet.contains hashes straight to the right bucket (O(1) on average). Inside a loop, that's the difference between O(nยฒ) and O(n):

Java contains on a List vs a Set

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The set is hundreds of times faster here, and the gap keeps growing with the size: double the data and the list version takes four times as long. Typical fixes of this kind:

  • Lookups by key or "have I seen this?" checks: a HashMap or HashSet instead of searching a list.
  • Removing from the front of a list in a loop: an ArrayDeque instead of ArrayList.remove(0).
  • Repeatedly finding the smallest element: a PriorityQueue instead of sorting again.
  • Presize big collections when you know the size: new ArrayList<>(expected), or HashMap.newHashMap(expected) (Java 19), to avoid repeated resizing.

Choosing the Right Collection has the full big-O table.

Don't repeat work: caching and hoisting

The fastest code is code that doesn't run. Look for work that's repeated with the same inputs:

  • Hoist invariant work out of loops: compute list.size(), a parsed config value or a formatter once, before the loop.
  • Precompile regular expressions. String.matches, replaceAll and split (with a real regex) compile a Pattern on every call. Store the Pattern in a static final field.
  • Reuse expensive, thread-safe objects: a DateTimeFormatter, an ObjectMapper, an HttpClient. Creating one per call is a common hidden cost.
  • Cache results of expensive pure functions with Map.computeIfAbsent (memoization).
Java Precompiled patterns and a memoization cache

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Twelve orders needed only three slow calls, one per distinct zone. In real applications, caches need limits and expiry (a LinkedHashMap LRU from LinkedHashMap and LinkedHashSet, or a library like Caffeine); an unbounded cache is a memory leak with good intentions.

I/O is usually the real bottleneck

A CPU executes billions of instructions per second; a disk read or a network round trip takes thousands to millions of times longer than an addition. In business applications, the slow parts are almost always outside your loops:

  • Buffer I/O. Read files with BufferedReader or Files.lines, write with BufferedWriter. Unbuffered byte-by-byte I/O makes one system call per byte (Byte and Character Streams).
  • Batch database work. One query that returns 1,000 rows beats 1,000 queries that return one row each (the "N+1 queries" problem).
  • Don't log in hot loops at enabled levels, and keep disabled messages cheap (Logging).
  • Do independent waits concurrently. Three remote calls of 100 ms each take 300 ms in sequence but about 100 ms in parallel, which is where virtual threads and CompletableFuture shine.

What the JIT already does for you

Modern JVMs (HotSpot, the JVM in almost every JDK) compile code in tiers: methods start in the interpreter, hot ones are compiled quickly by C1, and the hottest are recompiled with heavy optimizations by C2, using profiling data gathered while the code ran. Knowing a few of those optimizations saves you from useless "tricks":

  • Inlining. Small methods, such as getters, are copied into their callers, so calling them is free. Write small, clear methods; don't inline by hand.
  • Escape analysis. A short-lived object that never leaves a method may never be allocated on the heap at all. Small temporary objects (records, iterators) are cheap; garbage collection of young objects is fast too.
  • Bounds-check and null-check elimination, loop unrolling, vectorization. Plain for loops over arrays are compiled into very efficient machine code.
  • Speculation and deoptimization. The JIT optimizes for what it has seen (for example, that only one implementation of an interface is ever used) and quietly falls back if that changes.

Consequences: write straightforward code; don't reuse objects "to save allocations" unless a profiler shows allocation is the problem; avoid exceptions for normal control flow (creating a stack trace is expensive); and remember that short programs mostly measure startup, not peak speed. For startup-sensitive applications, look at Class Data Sharing and the ahead-of-time cache (Java 24 and 25 added -XX:AOTCacheOutput / -XX:AOTCache to store loaded and profiled classes from a training run).

Don't reach for parallel streams by default. parallelStream() splits work across cores, which helps only for large, CPU-heavy, independent work. For small collections or I/O-bound tasks it's often slower, and it shares one common thread pool with the whole application. Measure first (Advanced Streams).

Profiling: finding the real hot spots

For a whole application, timing individual methods by hand doesn't scale. A profiler samples what every thread is doing many times per second and shows where time and memory go:

Terminal
# Java Flight Recorder: built into the JDK, low overhead, safe in production
java -XX:StartFlightRecording=duration=60s,filename=app.jfr -jar app.jar

# Start a recording in an already running JVM (find its id with jcmd)
jcmd 12345 JFR.start duration=60s filename=app.jfr

# Print the hottest methods from a recording
jfr print --events jdk.ExecutionSample app.jfr | head -50

# GC activity, one line per collection
java -Xlog:gc -jar app.jar

Open .jfr files in JDK Mission Control for flame graphs, allocation hot spots, lock contention and GC pauses. Other popular tools are async-profiler (flame graphs with very low overhead), VisualVM and the profilers built into IntelliJ IDEA. Thread dumps, heap dumps and jcmd are covered in Debugging and Profiling Tools.

A profile often surprises you: the time goes into JSON serialization, a regex in a validation method, a logging call or a database query, rarely into the loop you were worried about. Fix the top item, measure again, repeat.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You time a method once with System.nanoTime() right after the program starts. What does the number mostly include?

    The JVM interprets code first and compiles hot methods later. A single early measurement mostly measures warm-up; benchmarks should run the code many times first and then take several measurements.
  2. 2. Why is this slow? Long total = 0L; for (int i = 0; i < n; i++) total += i;

    Wrapper objects are immutable, so each iteration allocates a new Long. Using the primitive long avoids millions of allocations.
  3. 3. Which line is a real performance problem?

    Repeated += on a String in a loop copies the whole string each time, which is quadratic. A single concatenation expression is compiled efficiently and is fine.
  4. 4. A loop checks blocked.contains(id) for 100,000 ids, and blocked is an ArrayList of 100,000 elements. What is the best fix?

    ArrayList.contains scans the list (O(n)), so the loop is O(nยฒ). HashSet.contains is O(1) on average, which turns the whole loop into O(n).
  5. 5. What is the first step when an application is too slow?

    Guesses are usually wrong. Profile the real workload, fix the biggest hot spot, and measure again to confirm the change helped.

Summary

  • Measure before and after every optimization. Use System.nanoTime() for durations, warm up, repeat, take the median and always use the results; use JMH for serious micro-benchmarks.
  • Avoid boxing in hot code: primitive counters, arrays and IntStream instead of Long accumulators and List<Integer>.
  • Never build strings with += in a loop; use StringBuilder, String.join or Collectors.joining. Single concatenation expressions are fine.
  • The right collection beats any micro-trick: HashSet/HashMap for lookups, ArrayDeque for queues, presized collections for known sizes.
  • Don't repeat work: hoist invariants, precompile regex Patterns, reuse expensive objects and cache pure results (with limits).
  • I/O, databases and network calls usually dominate: buffer, batch and run independent waits concurrently.
  • Trust the JIT with small methods and short-lived objects; find real hot spots with a profiler such as Java Flight Recorder and Mission Control.

Next: Best Practices and Common Mistakes, a checklist of habits that keep Java code correct, readable and safe.