Quick revision
The short answer
Softly reachable objects are kept until the collector decides it needs the space, so a cache of SoftReference values grows until it fills the heap. Each collection then has to process many soft references, the old generation stays full, and collections become frequent and expensive, while the JVM avoids an OutOfMemoryError by clearing soft references just in time. Because the clearing policy depends on free heap and last access time, through SoftRefLRUPolicyMSPerMB, behaviour is hard to predict; the fix is an explicitly bounded cache such as Caffeine with maximumSize or maximumWeight and expiry, sized from measured entry size and a memory budget, with hit rate and size metrics.
What to remember
In this answer, we saw that a SoftReference cache grows until the heap is nearly full, because softly reachable objects are only cleared when the collector needs space. The old generation stays full, marking runs constantly, mixed collections free little and reference processing adds work, so GC becomes frequent and expensive, while clearing just in time avoids an OutOfMemoryError. The SoftRefLRUPolicyMSPerMB policy ties survival to free heap and last access, making behaviour unpredictable. We replaced it with a Caffeine cache bounded by weight and expiry, sized from measured entry size and a memory budget, and monitored through hit rate and size metrics.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Softly reachable objects are kept until the collector decides it needs the space, so a cache of SoftReference values grows until it fills the heap. Each collection then has to process many soft references, the old generation stays full, and collections become frequent and expensive, while the JVM avoids an OutOfMemoryError by clearing soft references just in time. Because the clearing policy depends on free heap and last access time, through SoftRefLRUPolicyMSPerMB, behaviour is hard to predict; the fix is an explicitly bounded cache such as Caffeine with maximumSize or maximumWeight and expiry, sized from measured entry size and a memory budget, with hit rate and size metrics.
Why this question is asked
Soft references look like a free lunch: cache everything, and let the JVM throw entries away when memory gets tight. In production the result is usually the opposite of free. The heap sits near full, GC runs constantly, latency gets worse, and nobody can say how big the cache is or how many requests it actually serves. Interviewers ask this to see whether we understand how the collector treats soft references and why explicit limits beat implicit ones. In this answer, we will recap soft references, see why this cache fills the heap, understand why GC gets slower, look at the clearing policy, and replace the cache with a bounded one.
1A Quick Recap of Soft References
A SoftReference holds its referent in a way that does not prevent collection under memory pressure. The JVM promises two things: softly reachable objects may be cleared whenever the collector decides it needs memory, and all of them will be cleared before the JVM throws OutOfMemoryError. Everything in between is up to the collector.
In our service the map holds keys strongly and each Report through a SoftReference. So nothing limits the cache except the collector's decisions.
2Why the Cache Fills the Heap
Every computed report goes into the map, and nothing removes it. As long as the heap has free space, the collector has no reason to clear soft references, so the reports stay. They survive young collections, get promoted to the old generation, and the old generation grows until it is close to full.
Only then does the collector start clearing some soft references, enough to keep going, and the cache immediately refills them as new reports are computed. The heap settles in a state where it is always nearly full of reports, with a little room at the top. That is the state the question describes.
3Why GC Gets Slower
A heap that is always nearly full is the worst case for a generational collector. In G1, old occupancy stays above the marking threshold, so concurrent marking cycles run back to back. Mixed collections find old regions full of live reports, which are expensive to evacuate and free little space. The young generation gets squeezed, so young collections become more frequent.
On top of that, every collection that discovers soft references must process them: decide which to clear, clear them and enqueue them. With hundreds of thousands of entries, that reference processing adds to pause times, visible in detailed GC logs as reference processing phases. Concurrent GC threads also stay busy, which is why GC CPU rises. Nothing fails, because the collector clears just enough soft references to avoid an OutOfMemoryError, but the service pays a constant GC tax.
- Reports cached via SoftReferencenothing removes them
- Heap has free space?YesCollector keeps all soft referentsNoCollector clears some soft referents
- Old generation fills with reportspromoted, long-lived
- Marking runs constantly, mixed GCs free littleGC CPU rises
- Cache recomputes and refills cleared entriesback to a full heap
5 steps in order. Play them, or step through them one at a time.
4The Clearing Policy Is Hard to Predict
HotSpot decides which soft references to clear with an LRU-like rule controlled by -XX:SoftRefLRUPolicyMSPerMB, 1000 by default. A softly reachable object is kept if it was accessed more recently than 1,000 ms per megabyte of free heap.
Notice what this means. How long an entry survives depends on how full the heap is at that moment, which depends on traffic, other allocations, and the cache itself. Eviction is done in bulk by the collector at GC time, not by the cache. So we cannot say how many entries the cache holds, how much memory it uses or what its hit rate will be. Free heap is measured differently by different collectors and settings, so the exact numbers vary; check your version.
5Replacing It With a Bounded Cache
The fix is to decide the cache's limits ourselves. Caffeine, the standard caching library on the JVM and Spring Boot's preferred cache provider, lets us bound a cache by entry count or by weight, expire entries by time, and record statistics.
Now the cache uses at most about 512 MB, evicts by Caffeine's frequency-aware policy when full, and expires entries after 15 minutes regardless of memory. The collector sees an ordinary, stable set of live objects and goes back to normal behaviour.
Sizing comes from measurements, not guesses. If a heap dump or a sample shows an average report of about 200 KB, a 512 MB budget holds roughly 2,600 reports. If the heap is 4 GB and the rest of the service needs about 1.5 GB live, a 512 MB cache leaves ample headroom. Then we expose the cache's size, hit rate and eviction count as metrics, through Micrometer's Caffeine binder or Spring's cache metrics, and adjust the budget based on how the hit rate responds.
6Follow-up questions
Why not just lower SoftRefLRUPolicyMSPerMB?
Lowering it makes the collector clear soft references sooner, which reduces the heap the cache takes, but it remains a global JVM setting tied to free heap and GC timing. It affects every soft reference in the process, including those in libraries, and it still gives no control over cache size or hit rate. It treats the symptom; a bounded cache fixes the design.
Is Caffeine's softValues() option any better?
It is the same mechanism with the same problems, which is why Caffeine's own guidance recommends maximumSize or maximumWeight instead. Soft values in Caffeine are only reasonable as a last-resort safety net in addition to a size bound.
How would you confirm that the soft-reference cache causes the GC load?
Detailed GC logs show reference processing time and the number of soft references discovered at each collection; a heap dump shows the map and its SoftReference values with a retained size close to the old generation. A canary with the bounded cache should then show old occupancy dropping and GC CPU falling.
7Conclusion
In this answer, we saw that a SoftReference cache grows until the heap is nearly full, because softly reachable objects are only cleared when the collector needs space. The old generation stays full, marking runs constantly, mixed collections free little and reference processing adds work, so GC becomes frequent and expensive, while clearing just in time avoids an OutOfMemoryError. The SoftRefLRUPolicyMSPerMB policy ties survival to free heap and last access, making behaviour unpredictable. We replaced it with a Caffeine cache bounded by weight and expiry, sized from measured entry size and a memory budget, and monitored through hit rate and size metrics.
Open with
The problem is that soft references have no limit except the collector's judgement. Every report goes into the map and stays until the collector decides it needs memory, so the cache grows until the heap is nearly full.
Then, if they want more
The old generation is then permanently full of live reports, so G1 runs concurrent marking back to back, mixed collections find mostly live data and free little, young collections get squeezed, and every collection spends time processing hundreds of thousands of soft references.
It never throws OutOfMemoryError because the JVM clears soft references just in time, so the symptom is a full heap and high GC CPU rather than a crash.
On top of that, HotSpot's clearing rule, SoftRefLRUPolicyMSPerMB, keeps a soft object if it was used within about one second per megabyte of free heap, so how long an entry lives depends on how full the heap is at that moment, which makes the cache's size and hit rate unpredictable.
I would replace it with an explicitly bounded cache, Caffeine with maximumWeight and a weigher, or maximumSize, plus expireAfterWrite.
I would size it from the measured average report size and a memory budget, say 512 MB for 200 KB reports, which is about 2,600 entries, and expose hit rate, size and evictions as metrics so we can tune the budget from data.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- HotSpot Garbage Collection Tuning Guide, JDK 21
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A service caches computed reports in a ConcurrentHashMap<String, SoftReference<Report>>....": https://scrivio-interview-library.netlify.app/java/garbage-collection/66789cd5a5/