Sub topic 11
Allocation rate and GC pressure
This chapter assumes you can read a GC log (covered earlier), know the difference between the young and old generations, and can open a JFR recording in JDK Mission Control.
Why allocation matters
Allocating an object on the JVM is cheap. The cost shows up later. Every object that dies young still fills the young generation, and each time it fills, the collector runs a pause. A high allocation rate (bytes allocated per second) therefore causes frequent young collections. This is GC pressure.
You can see it in the GC log from the previous chapter. If young collections occur many times per second, or the share of time spent in GC is high, the application is allocating faster than it needs to.
A program that allocates heavily
The Slow program from earlier builds a new String on every +=. Each iteration copies the whole string so far and discards the old one. With 5,000 iterations per round, that produces large amounts of short-lived garbage.
public class Slow {
public static void main(String[] args) {
long start = System.nanoTime();
for (int round = 0; round < 200; round++) {
String s = "";
for (int i = 0; i < 5_000; i++) {
s += i;
}
if (s.length() == 0) {
System.out.println("unreachable");
}
}
System.out.println((System.nanoTime() - start) / 1_000_000 + " ms");
}
}Measuring allocation
Two quick checks give a first estimate:
- Run
jstat -gcutil <pid> 1000and watch how fast the young-generation columns (Efor Eden) fill and reset, and how the young collection count (YGC) grows. - Run with
-Xlog:gc*and count the young collections per second.
For the cause, use JFR. Record with the profile settings, then open the recording in JDK Mission Control and go to the memory view. Look at these:
- Allocation by class shows which types account for the most bytes.
- Allocation by thread shows which threads allocate most.
- Allocation by stack trace shows which call sites allocate.
JFR does not record every allocation. It samples them, mostly when a thread fills a new allocation buffer. Treat the numbers as proportions, not exact counts.
Reading the result
For Slow, the allocation view shows char[] or byte[] arrays (the string contents, depending on JDK version) and String objects at the top. The stack trace points to the s += i line in main. That is the call site to change.
Reducing allocation
Fix the hot call sites first. Common changes are:
- Use
StringBuilderfor repeated concatenation. - Presize collections and builders when you know the size.
- Avoid boxing in hot loops (
longinstead ofLong). - Reuse immutable or thread-safe objects instead of creating them per call.
Do not reach for object pools by default. Short-lived objects are cheap for the collector, and pools add complexity and can promote objects to the old generation.
Exercise
A GC log shows a young collection every 50 ms, each freeing almost all of Eden, and the old generation stays flat. Is this a memory leak? What would you check next?
Show answer
No. Flat old-generation usage and near-empty Eden after each collection show short-lived garbage, which means a high allocation rate rather than a leak. Next, record with JFR and look at allocation by stack trace to find the call sites that allocate the most bytes.