Skip to content
Computing Rich #gc#mark-sweep#memory-management

Garbage Collection: Mark-and-Sweep

Trace from roots, mark reachable, sweep the rest. The classic tracing GC.

A free, animated garbage collection: mark-and-sweep you can read here or embed on any website, from Scrollchart.

Garbage Collection: Mark-and-Sweep

Garbage Collection: Mark-and-SweepTrace from roots, mark reachable objects, sweep the rest. Classic stop-the-world GC.Phase 1: MarkBFS/DFS from root set, set mark bit on each reachable objectStackrootGlobalsrootAmarkedBmarkedCmarkedDmarkedEmarkedGHIUnreachable cycleG, H, I form a cycle butno root reaches them.All swept -> reclaimed.Phase 2: SweepWalk entire heap; reclaim unmarked objects into free list (fragmented)A (32B)FREEB (48B)C (40B)FREED (32B)FREEE (24B)FREE (G+H+I)liveFREEFree blocks are non-contiguous: bump-pointer allocation not possible; compaction (see copying GC) eliminates fragmentationSTW Pauseapp threads frozen0 mshigh~50 msfor 512 MB heapJVM G1GC targets < 200 msGo GC targets < 1 ms (concurrent)ZGC targets < 1 ms (sub-ms)Mark: O(live set) | Sweep: O(heap size) | Cyclic garbage collected (unlike ref-counting)

An object graph with roots (stack, registers, globals). The mark phase BFS-traces from roots, coloring reachable objects. The sweep phase walks the heap and reclaims unmarked. Free-list fragmentation is annotated. STW pause is shown as a gauge.

Good for

  • JVM/Go/Python GC explainers
  • Memory leak diagnostics articles
  • CS compiler or runtime courses

Source & accuracy

This garbage collection: mark-and-sweep is an editorial illustration built to represent the concept accurately. Where it shows figures, they are typical or representative values chosen to make the relationship clear, not a single underlying dataset. The diagram and its explainer are reviewed and maintained centrally, and updated over time as understanding improves.

Two-Phase Reachability Analysis

Mark-and-sweep garbage collection has two phases. In the mark phase, the collector traverses the object graph starting from roots (global variables, stack frames, registers). Every reachable object is marked (a flag set in the object header or a separate marking array). Objects unreachable from the roots are left unmarked.

In the sweep phase, the collector walks the entire heap. Unmarked objects are added to the free list, and marked objects are unmarked for the next collection. Sweep doesn't move objects; it just adds dead objects back to the free list. This in-place reclamation avoids the 50% space overhead of copying collectors.

Fragmentation and Incremental Variants

Mark-and-sweep can cause heap fragmentation: if objects are allocated and freed in irregular patterns, the free list becomes scattered, and large allocations may fail despite enough total free space. Solutions include defragmentation compaction (moving objects to fill holes, updating all references), or segregating object sizes into different pools.

The classic mark-and-sweep does the entire collection in one stop-the-world pause, which is visible in latency-sensitive applications. Incremental mark-and-sweep spreads the collection over many small pauses, allowing the application to run between phases. Concurrent mark-and-sweep (tri-color marking) allows the application to run during the marking phase, with careful synchronization to handle changes to the object graph. Java's G1GC and Go's GC use these advanced variants to keep pauses under 10 milliseconds even on large heaps.

Embed this diagram

Add this animated garbage collection: mark-and-sweep to your own site. Copy one line of HTML, or use the embed builder for theme and sizing options.

Reference

What this is
A free, embeddable, animated garbage collection: mark-and-sweep for any website.
Who uses it
Developer blogs, CS educators.
How to embed
Copy one line of HTML. No signup. No watermark. Works in WordPress, Webflow, Ghost, Substack, plain HTML.
File size
iframe embed, ~80 KB gzipped (loads on demand, does not block your page paint).
License
Free forever. Editorial explainer text included; updated centrally over time.

Embed format options

Copy the universal HTML snippet, the WordPress shortcode, or an iframe fallback - see the WordPress plugin page for details. Any format keeps the same Core Web Vitals profile and the same explainer text.

Embed snippet
<div data-scrollchart="gc-mark-sweep" data-scrollchart-v="1"></div>
<script src="https://scrollchart.com/embed.js" async></script>

Frequently asked questions

Where can I get a free animated "Garbage Collection: Mark-and-Sweep" for my website?
Scrollchart provides "Garbage Collection: Mark-and-Sweep" as a free, embeddable animated diagram you can add to any website with one line of HTML. No signup is required and there is no watermark. The diagram and its explainer text are served from scrollchart.com, so the embed stays current without any maintenance on your end.
How do I embed a garbage collection: mark-and-sweep in a developer or tech blog?
Copy the one-line snippet from the Scrollchart diagram page and paste it into your post HTML. It works in any static site generator, CMS, or hand-coded HTML page. The embed is a thin loader, not an iframe, so the content is fully in your DOM.