Quick revision
The short answer
Mark-sweep marks the reachable objects and puts the rest on free lists without moving anything, which is cheap per cycle but fragments the heap and slows allocation. Mark-compact marks and then slides live objects together, so the heap stays unfragmented and allocation is a fast pointer bump, at the cost of more work per cycle. Copying evacuates live objects into another space, costs time proportional only to live data and compacts for free, but needs spare space. HotSpot copies in the young generation and compacts or evacuates in the old one.
What to remember
In this answer, we saw that mark-sweep frees memory in place and leaves fragmentation, mark-compact slides live objects together at the cost of extra passes, and copying moves survivors into an empty space, costing only live data but needing spare space. HotSpot copies in the young generation because most objects die young, and in the old generation uses mark-compact in Serial and Parallel, region evacuation in G1, and concurrent relocation in ZGC and Shenandoah. CMS, the mark-sweep exception, fragmented and was removed in JDK 14.
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.
Mark-sweep marks the reachable objects and puts the rest on free lists without moving anything, which is cheap per cycle but fragments the heap and slows allocation. Mark-compact marks and then slides live objects together, so the heap stays unfragmented and allocation is a fast pointer bump, at the cost of more work per cycle. Copying evacuates live objects into another space, costs time proportional only to live data and compacts for free, but needs spare space. HotSpot copies in the young generation and compacts or evacuates in the old one.
Why this question is asked
These three algorithms are the building blocks of every Java collector, and almost every GC behaviour we see in production, long full collections, fragmentation, allocation stalls, comes back to which one is running. Interviewers ask this to check that we understand why HotSpot is designed the way it is, not only the names of its collectors. In this answer, we will recap reachability and marking, walk through each algorithm on the same small heap, compare their trade-offs, and then map them onto the HotSpot collectors, including CMS and why it was removed.
1A Quick Recap of Marking
All three algorithms start with the same question: which objects are still in use? An object is in use if it is reachable, which means there is a chain of references to it from a root. Roots are references the JVM knows are live, such as local variables on thread stacks, static fields and JNI handles.
Marking is the traversal that answers the question. The collector starts at the roots, follows every reference, and sets a mark bit on each object it reaches. When marking ends, every unmarked object is garbage. What the three algorithms differ in is what they do next with the memory.
2Mark-Sweep: Free in Place
After marking, a sweep walks the whole heap. Every unmarked object's space is added to a free list, a list of holes with their sizes. Nothing moves, so references never need updating, and the sweep can even run alongside the application.
The cost appears later. The free memory is scattered in holes of different sizes, which is called fragmentation. A new 2 MB array might not fit anywhere even when 500 MB is free in total. Allocation also gets slower, because the allocator has to search the free lists for a hole big enough instead of simply taking the next address.
3Mark-Compact: Slide Live Objects Together
Mark-compact marks the same way, then moves every live object toward one end of the space, keeping their order, and updates every reference to point to the new addresses. The free space becomes one contiguous block. So there is no fragmentation, and allocation is a bump of a pointer: the next object goes at the current top, and top moves up by its size.
The price is work. Compaction usually needs several passes over the heap: computing new addresses, updating references and moving objects. Done while the application is stopped, as in the full collections of Serial and Parallel GC, the pause grows with the amount of live data, which is why a large old generation can pause for seconds.
4Copying: Evacuate the Survivors
A copying collector divides memory into spaces. It allocates in one space, and when that fills, it copies every live object it reaches from the roots into an empty space, leaving the old space entirely garbage, which it then reuses whole. It never looks at dead objects at all. So its cost depends on how much is alive, not on how big the space is, and the result is compacted automatically.
But there is a catch: the destination space must be large enough to receive the survivors, so some memory always sits empty waiting for the next copy. A naive semispace design wastes half the memory. That is acceptable only where few objects survive, which brings us to the young generation.
| Algorithm | Fragmentation | Cost depends on | Memory overhead |
|---|---|---|---|
| Mark-sweep | yes, free lists of holes | heap size (sweep) and live data | low |
| Mark-compact | none, bump-pointer allocation | live data, several passes | low |
| Copying | none, compacts as it copies | live data only | needs an empty target space |
5What HotSpot Uses Where
HotSpot applies the generational idea: it allocates new objects in a young generation and promotes survivors to an old generation. For the young generation every HotSpot collector uses copying. New objects go into eden; a young collection copies the live ones into a survivor space, and objects that survive enough collections are copied into the old generation. Because most young objects die quickly, very little is copied, and the survivor spaces can be small.
For the old generation the choices differ. Serial and Parallel GC use a stop-the-world mark-compact. G1 marks the old generation concurrently and then evacuates selected regions, copying their live objects into free regions, which is copying applied region by region. ZGC and Shenandoah mark and relocate objects concurrently, so even moving old objects does not need a long pause.
CMS, the Concurrent Mark Sweep collector, used mark-sweep for the old generation so that it could sweep while the application ran. That gave short pauses, but it never compacted, so the old generation fragmented over time. When a promotion found no hole large enough, CMS fell back to a long single-threaded full compaction. CMS was deprecated in JDK 9 and removed in JDK 14.
| Collector | Young generation | Old generation |
|---|---|---|
| Serial | copying (stop-the-world) | mark-compact (stop-the-world) |
| Parallel | copying, parallel | mark-compact, parallel |
| G1 | copying of young regions | concurrent mark, then region evacuation |
| ZGC, Shenandoah | concurrent copying | concurrent mark and relocation |
| CMS (removed in JDK 14) | copying | concurrent mark-sweep, fragmented |
6Follow-up questions
Why does the weak generational hypothesis favour copying for young objects?
The hypothesis says most objects die young. Copying costs time only for live objects and ignores dead ones, so in a young generation where perhaps 2 to 5 percent survive a collection, copying touches very little memory. Its main drawback, needing an empty target space, is cheap here because the survivors are few, so the survivor spaces can be small. Mark-sweep or mark-compact would have to deal with the whole space including the dead objects.
Why is bump-pointer allocation so fast?
Allocating is just adding the object size to a pointer and checking that it did not pass the end of the buffer. Each thread gets its own buffer in eden, a TLAB (thread-local allocation buffer), so it does not even need synchronisation. A free-list allocator instead has to search for a suitable hole and may split it.
Does G1 ever run a full mark-compact?
Yes, as a fallback. If evacuation cannot find free regions, or mixed collections cannot keep up, G1 runs a full GC, which has been parallel since JDK 10. A full GC in G1 is a sign of too little headroom or a very high promotion rate, and it should be investigated rather than accepted.
7Conclusion
In this answer, we saw that mark-sweep frees memory in place and leaves fragmentation, mark-compact slides live objects together at the cost of extra passes, and copying moves survivors into an empty space, costing only live data but needing spare space. HotSpot copies in the young generation because most objects die young, and in the old generation uses mark-compact in Serial and Parallel, region evacuation in G1, and concurrent relocation in ZGC and Shenandoah. CMS, the mark-sweep exception, fragmented and was removed in JDK 14.
Open with
All three algorithms first mark: they start from roots like thread stacks and static fields and mark every reachable object. Mark-sweep then frees unmarked objects in place into free lists.
Then, if they want more
Nothing moves, so it is cheap per cycle, but the heap fragments, a large object may not fit even with plenty of free memory, and allocation has to search free lists.
Mark-compact slides live objects together and updates references, so there is no fragmentation and allocation is a simple pointer bump, but it takes several passes and, done stop-the-world, the pause grows with live data.
Copying evacuates live objects into an empty space; it never touches dead objects, so the cost depends only on live data and it compacts for free, but it needs spare space. HotSpot uses copying for the young generation in every collector, eden into survivor spaces, because most objects die young, so little is copied.
For the old generation, Serial and Parallel use stop-the-world mark-compact, G1 marks concurrently and evacuates selected regions, and ZGC and Shenandoah mark and relocate concurrently. CMS used concurrent mark-sweep for the old generation, fragmented over time, and was removed in JDK 14.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 14, Java 10, Java 9
- 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 "Explain mark-sweep, mark-compact and copying collection. What are the trade-offs in...": https://scrivio-interview-library.netlify.app/java/garbage-collection/46a8b8a92b/