Video in progress

Strong Middle ⚙️ JVM and memory

What was the CMS collector, why did it have a fragmentation problem, and in which version was it removed?

CMS (Concurrent Mark Sweep) marked the old gen concurrently with the application and cleaned the garbage in place, but didn't move objects (no compaction). Over time the old gen would fragment, and if no space could be found for a big object, a long single-threaded Full GC followed. It was deprecated in JDK 9 and removed in JDK 14 (JEP 363).

📖 Answer

CMS used to be the only choice for low latency. Its cycle: a short initial mark pause → concurrent mark → a short remark pause → concurrent sweep. The sweep added the dead areas to a free list.

The problem is in the word "sweep". Objects are not moved, so after a few days the old gen looks like Swiss cheese: 2 GB free in total, but the largest contiguous chunk is 1 MB. A young GC can't promote an object ("promotion failed" or "concurrent mode failure"), and a single-threaded compacting Full GC in the Serial style begins. On an 8 GB heap this could take tens of seconds.

Other drawbacks:

  • allocation from a free list is slower than bump-the-pointer;
  • the concurrent work competes with the application for CPU;
  • far too many tuning flags (CMSInitiatingOccupancyFraction and others).

G1 solved the same goal with regions and evacuation (moving), so there is no fragmentation. Then ZGC and Shenandoah brought concurrent compaction. There was no reason left to keep the CMS code.

The practical side: today you need to know about CMS in two places. The first is when migrating old Java 8 services: JDK 14 ignores -XX:+UseConcMarkSweepGC with a warning, and JDK 15+ won't start with it at all (Unrecognized VM option). The second is in an interview, as the best example for the question "why does compaction matter".