I was curious to measure time spent allocating memory in JDK 13 using G1 and Epsilon. The results I have observed are unexpected and I'm interested in understanding what's going on. Ultimately, I'm looking to understand how to make Epsilon usage more performant than G1 (or if that isn't possible, why).
I wrote a small test that allocates memory repeatedly. Depending on command-line input it will either:
- create 1,024 new 1 MB arrays, or
- create 1,024 new 1 MB arrays, measure the time around the allocation, and print out the per-allocation elapsed time. This isn't measuring just the allocation itself, and does include time elapsed for anything else that occurs between the two calls to
System.nanoTime()
- still, it seems to be a useful signal to listen to.
Here's the code:
public static void main(String[] args) {
if (args[0].equals("repeatedAllocations")) {
repeatedAllocations();
} else if (args[0].equals("repeatedAllocationsWithTimingAndOutput")) {
repeatedAllocationsWithTimingAndOutput();
}
}
private static void repeatedAllocations() {
for (int i = 0; i < 1024; i++) {
byte[] array = new byte[1048576]; // allocate new 1MB array
}
}
private static void repeatedAllocationsWithTimingAndOutput() {
for (int i = 0; i < 1024; i++) {
long start = System.nanoTime();
byte[] array = new byte[1048576]; // allocate new 1MB array
long end = System.nanoTime();
System.out.println((end - start));
}
}
Here is the version info for JDK I'm using:
$ java -version
openjdk version "13-ea" 2019-09-17
OpenJDK Runtime Environment (build 13-ea+22)
OpenJDK 64-Bit Server VM (build 13-ea+22, mixed mode, sharing)
Here are the different ways I'm running the program:
- allocation only using G1:
$ time java -XX:+UseG1GC Scratch repeatedAllocations
- allocation only, Epsilon:
$ time java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC Scratch repeatedAllocations
- allocation + timing + output using G1:
$ time java -XX:+UseG1GC Scratch repeatedAllocationsWithTimingAndOutput
- allocation + timing + output, Epsilon:
time java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC Scratch repeatedAllocationsWithTimingAndOutput
Here are some timings from running G1 with allocations only:
$ time java -XX:+UseG1GC Scratch repeatedAllocations
real 0m0.280s
user 0m0.404s
sys 0m0.081s
$ time java -XX:+UseG1GC Scratch repeatedAllocations
real 0m0.293s
user 0m0.415s
sys 0m0.080s
$ time java -XX:+UseG1GC Scratch repeatedAllocations
real 0m0.295s
user 0m0.422s
sys 0m0.080s
$ time java -XX:+UseG1GC Scratch repeatedAllocations
real 0m0.296s
user 0m0.422s
sys 0m0.079s
Here are some timings from running Epsilon with allocations only:
$ time java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC Scratch repeatedAllocations
real 0m0.665s
user 0m0.314s
sys 0m0.373s
$ time java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC Scratch repeatedAllocations
real 0m0.652s
user 0m0.313s
sys 0m0.354s
$ time java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC Scratch repeatedAllocations
real 0m0.659s
user 0m0.314s
sys 0m0.362s
$ time java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC Scratch repeatedAllocations
real 0m0.665s
user 0m0.320s
sys 0m0.367s
With or without timing+output, G1 is faster than Epsilon. As an additional measurement, using the timing numbers from repeatedAllocationsWithTimingAndOutput
, the average allocation times are larger when using Epsilon. Specifically, one of the local runs showed G1GC averaged 227,218 nanos per allocation, whereas Epsilon averaged 521,217 nanos (I captured the output numbers, pasted into a spreadsheet, and used the average
function for each set of numbers).
My expectation was that the Epsilon tests would be observably faster, however in practice I'm seeing ~2x slower. The max allocation times are definitely higher with G1, but only intermittently – most of the G1 allocations are significantly slower than Epsilon, almost one order of magnitude slower.
Here is a plot of the 1,024 times from running repeatedAllocationsWithTimingAndOutput()
with G1 and Epsilon. The dark green is for G1; light green is for Epsilon; Y-axis is "nanos per allocation"; Y-axis minor gridlines every 250,000 nanos. It shows the Epsilon allocation times are very consistent, around 300-400k nanos each time. It also shows the G1 times are significantly faster most of the time, but also intermittently ~10x slower than Epsilon. I'm assuming this would be attributable to the garbage collector running, which would be sane and normal, but also seems to negate the idea that G1 is smart enough to know it doesn't need to allocate any new memory.
array
is not used, so it becomes eligible for garbage collection, and then the old memory is just reused, while Epsilon might have to ask the OS for more memory. tl;dr: This test doesn't show anything.sys
time. Perfectly plausible results.