Experiments and implementations · Intermediate

Observe allocation and escape analysis

Measure Java thread allocation with consumed results, contrasting a local object with one published through a volatile field.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Predict allocation separately from source construction
  2. Fix the runtime and measurement interval
  3. Keep the two cases in separate methods
  4. Read the observation within its limits
  5. Recognize changes that invalidate the comparison
  6. Mark the measurement interval on the source
  7. Exercise: predict, run, and explain
  8. Ask Logeia about this articleCheck your understanding

Predict allocation separately from source construction

A new expression does not always require a separately allocated runtime object on every iteration. If the runtime proves that external code cannot observe the object and preserves program behavior, it may replace the object with computations on its fields. Compare two methods that construct the same Pair. One uses it locally; the other publishes it through a static volatile field.

Read the compiler/runtime and GC guides first. The metric here is bytes allocated by the current thread as reported by a JVM management API. We do not measure elapsed time, live bytes after collection, or process resident memory. Nor should the whole computation disappear because its result is unused. Both methods return a sum that is checked and printed, making result consumption an explicit experimental condition.

Fix the runtime and measurement interval

The recorded runtime is Ubuntu OpenJDK 64-Bit Server VM 17.0.20+8-1-24.04-Ubuntu. JDK 17 can launch the source file directly. The program uses the com.sun.management extension to ThreadMXBean, so not every Java implementation provides this counter. If unsupported, stop rather than relabeling another measurement as allocation bytes.

Warm both methods twenty times with one hundred thousand iterations each. Then measure three rounds of five hundred thousand iterations. Startup, source compilation, warmup, and printing lie outside each allocation-difference interval. This warmup count is a condition of the recorded run, not a guarantee that every JVM has finished optimizing. Print runtime details and flags, then launch the identical source in separate processes with escape analysis enabled and disabled. Keep the comparison focused on that change.

Keep the two cases in separate methods

Save the code as EscapeLab.java. Run java -XX:+DoEscapeAnalysis EscapeLab.java, then java -XX:-DoEscapeAnalysis EscapeLab.java. Preserve the separate local and escaped methods. A branch inside one shared loop would not guarantee that the analysis treats the two paths equivalently to separate methods.

Pair fields are long values and each iteration adds 2i + 1. Summing from zero through n − 1 yields n squared: 250000000000 for five hundred thousand iterations. The check uses (long)n * n to avoid overflowing an int calculation. The escaped method publishes its reference to sink on every iteration. Volatile writes have costs beyond allocation, but this experiment does not compare timings and cannot isolate allocation cost from a timing difference between these methods.

java
import java.lang.management.ManagementFactory;
import com.sun.management.ThreadMXBean;

public class EscapeLab {
    static final class Pair {
        final long x, y;
        Pair(long x, long y) { this.x = x; this.y = y; }
    }
    static volatile Object sink;
    static long local(int n) {
        long sum = 0;
        for (int i = 0; i < n; i++) {
            Pair p = new Pair(i, i + 1);
            sum += p.x + p.y;
        }
        return sum;
    }
    static long escaped(int n) {
        long sum = 0;
        for (int i = 0; i < n; i++) {
            Pair p = new Pair(i, i + 1);
            sink = p;
            sum += p.x + p.y;
        }
        return sum;
    }
    public static void main(String[] args) {
        ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
        if (!bean.isThreadAllocatedMemorySupported())
            throw new UnsupportedOperationException("allocation counter unavailable");
        bean.setThreadAllocatedMemoryEnabled(true);
        int n = 500_000;
        for (int i = 0; i < 20; i++) {
            local(100_000);
            escaped(100_000);
        }
        long thread = Thread.currentThread().getId();
        System.out.println(System.getProperty("java.vm.name") + " "
            + System.getProperty("java.runtime.version"));
        System.out.println("flags=" + ManagementFactory.getRuntimeMXBean().getInputArguments());
        for (int round = 1; round <= 3; round++) {
            long before = bean.getThreadAllocatedBytes(thread);
            long a = local(n);
            long localBytes = bean.getThreadAllocatedBytes(thread) - before;
            before = bean.getThreadAllocatedBytes(thread);
            long b = escaped(n);
            long escapedBytes = bean.getThreadAllocatedBytes(thread) - before;
            if (a != (long)n * n || b != a)
                throw new AssertionError("incorrect result");
            System.out.printf("round=%d sum=%d localBytes=%d escapedBytes=%d%n",
                round, a, localBytes, escapedBytes);
        }
    }
}

Read the observation within its limits

With escape analysis enabled, all three recorded rounds reported localBytes=0 and escapedBytes=16000000. With it disabled, both values were 16000000 in every round. Both configurations returned the same sum, 250000000000. This establishes that, in these runs, the local case's allocation counter changed while the checked calculation result stayed constant.

It does not establish that every local object moves onto a stack. The object may cease to exist as a separate allocation, leaving only values corresponding to its fields. Nor should sixteen million bytes be generalized into a universal object layout; headers, alignment, and configuration matter. If your observations differ, record the runtime, flags, warmup, and source changes. An optimization not observed must not be inserted into the report as an assumed fact.

Recognize changes that invalidate the comparison

Removing sum checks and output changes whether the computation is observable. Publishing only the final object changes which allocations escape. Printing inside the loop mixes output and string-related allocations into the measurement interval. These are changes to experimental conditions, not merely alternative formatting of equivalent evidence.

Allocations performed by another thread are not counted in the same way by this per-thread counter. Collection counts are not directly reported by the difference either. Record the current-thread scope, the before/after counter difference, and all three round values. Do not select only a favorable total. If the results vary, preserve the full series and change one condition, such as a longer warmup, before measuring again. Counter scope must remain part of the conclusion.

Mark the measurement interval on the source

Moving input construction or result formatting between the two counter reads would include their allocations in the difference. Mark the exact start and end lines and list the work between them. The result check occurs afterward, but its later use still keeps the calculation meaningful. Counter enabling and JVM startup are outside the reported interval too. This source-level annotation makes it possible to compare another run without silently measuring more work. It is especially useful if a seemingly harmless diagnostic statement is added during debugging and later left inside the measured region.

Exercise: predict, run, and explain

Change n to two hundred fifty thousand and calculate the expected sum before running. The answer is 62500000000. If object layout and optimization choices remain the same, predict approximately half the escaped allocation bytes. Put this prediction and the actual observation in separate fields. Do not assert that local allocation must be zero on every runtime: correctness assertions and observations of optimizer choices serve different purposes.

Finally compare allocated bytes with the live sets in the GC guide. A workload may allocate heavily while retaining little, or allocate less while a cache keeps objects alive for a long time. Explain which question each metric answers. The JIT guide then considers optimizations based on observed assumptions and what happens when execution can no longer rely on those assumptions.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 20

Choose a learning path