<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/garbage-collection-mark-and-sweep-xeuus4u0z" -->

---
title: Garbage Collection: Mark-and-Sweep | daily.dev
description: Mark-and-sweep garbage collection works in two phases: marking traverses all objects reachable from the garbage collector roots and flags them as alive, then...
canonical: https://daily.dev/posts/garbage-collection-mark-and-sweep-xeuus4u0z
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Garbage Collection: Mark-and-Sweep | daily.dev
og:description: Mark-and-sweep garbage collection works in two phases: marking traverses all objects reachable from the garbage collector roots and flags them as alive, then...
og:url: https://daily.dev/posts/garbage-collection-mark-and-sweep-xeuus4u0z
og:image: https://api.daily.dev/og/posts/XeUUs4U0z.png
og:image:alt: Garbage Collection: Mark-and-Sweep
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Garbage Collection: Mark-and-Sweep

**[navya](https://daily.dev/sources/dlrmprw88mjnbukqs8t0w)** · [@navyaak](https://daily.dev/navyaak) · 1 min read · 0 upvotes · 0 comments

## Summary

Mark-and-sweep garbage collection works in two phases: marking traverses all objects reachable from the garbage collector roots and flags them as alive, then sweeping scans the heap and reclaims memory from anything left unmarked. Programmers never explicitly delete objects; once nothing references an object, the collector automatically frees it. This simple two-step process underlies memory management in many managed-memory languages.

## Content

**Mark** — Start from the Garbage Collector roots and traverse all reachable objects, marking them as alive.

**Sweep** — Go through the heap and remove objects that were not marked.

In simple terms:

`Reachable → Keep ✅`
`Unreachable → Collect 🗑️`

The key idea is that an object doesn’t need to be explicitly deleted by the programmer.

If nothing can reach it anymore, the garbage collector can reclaim its memory.

A simple concept, but a fundamental piece of how managed-memory languages work.

## Similar posts on daily.dev

- [The Secret Life of JavaScript: The Garbage Collector](https://daily.dev/posts/the-secret-life-of-javascript-the-garbage-collector-r96mslcqu) · DEV · 8 upvotes · 1 comments
- [What garbage collection actually costs · Shivanshu Agrawal](https://daily.dev/posts/what-garbage-collection-actually-costs-shivanshu-agrawal-vptkcflye) · Hacker News · 7 upvotes · 0 comments
- [Baby’s Second Garbage Collector](https://daily.dev/posts/baby-s-second-garbage-collector-cjrbmht48) · Lobsters · 0 upvotes · 0 comments
- [Garbage Collection: Generational? Incremental? Both\! \(Python Language Summit 2026\)](https://daily.dev/posts/garbage-collection-generational-incremental-both-python-language-summit-2026--nybehi8cs) · Python Insider · 4 upvotes · 1 comments

---

[View this post on daily.dev](https://daily.dev/posts/garbage-collection-mark-and-sweep-xeuus4u0z)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/garbage-collection-mark-and-sweep-xeuus4u0z","headline":"Garbage Collection: Mark-and-Sweep","text":"Mark-and-sweep garbage collection works in two phases: marking traverses all objects reachable from the garbage collector roots and flags them as alive, then sweeping scans the heap and reclaims memory from anything left unmarked. Programmers never explicitly delete objects; once nothing references an object, the collector automatically frees it. This simple two-step process underlies memory management in many managed-memory languages.","url":"https://daily.dev/posts/garbage-collection-mark-and-sweep-xeuus4u0z","datePublished":"2026-09-04T18:19:15.585Z","dateModified":"2026-09-04T18:23:22.920Z","author":{"@type":"Person","name":"navya","url":"https://daily.dev/navyaak","image":"https://media.daily.dev/image/upload/s--vM6dTj_h--/f_auto/v1788852968/avatars/avatar_DLRmpRw88MjNBUKqs8T0W?_a=BAMAMicg0","description":"Software Engineer","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":420}},"image":"https://media.daily.dev/image/upload/s---z47hV7N--/f_auto/v1788546202/posts/XeUUs4U0z?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/sources/dlrmprw88mjnbukqs8t0w","name":"navya"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"navya","item":"https://daily.dev/sources/dlrmprw88mjnbukqs8t0w"},{"@type":"ListItem","position":3,"name":"Garbage Collection: Mark-and-Sweep"}]}
```

