Video in progress
What is premature promotion, and how does it make Old Gen fill up fast?
Short-lived objects get moved to Old Gen early, because they don't fit in Survivor or because young GCs happen too often. They die quickly there, but they are cleaned only by the more expensive old/mixed collections.
📖 Answer
Generational GC relies on the observation that "most objects die young". A new object is born in Eden. Each time it survives a young GC it is copied between the Survivor spaces and its age goes up. When it reaches MaxTenuringThreshold (usually 15), or when Survivor fills up, it moves to Old.
The problem starts here: a request creates objects that live 300 ms, but Eden is so small and the allocation rate so high that a young GC happens every 100 ms. The object hasn't died yet, it goes through 2-3 GCs, doesn't fit in Survivor and lands in Old. Old fills up fast, and mixed/full GCs become more frequent.
The signs in the GC log:
java -Xlog:gc*,gc+age=trace ...
- Old grows after every young GC, but the next old collection takes it almost back to where it started (meaning what was in there had "died young");
- in the
gc+agetable, objects are being moved at age 1-2; - lines like "Desired survivor size ... new threshold 1".
What you can do: make the young gen bigger (in G1 that usually means relaxing the pause target a bit, since G1 chooses the young size itself), reduce the allocation rate (big temporary arrays, needless DTO copies), and split requests into smaller chunks. With G1, setting -Xmn is not recommended: it removes the ability to adapt to the pause target.
Sign up to mark studied questions and save your progress.