<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq" -->

---
title: Writing a .NET Garbage Collector in C# - Part 4 | daily.dev
description: Part four of a series implementing a .NET garbage collector in C# covers how to walk the managed heap by computing object sizes from method table metadata, and...
canonical: https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Writing a .NET Garbage Collector in C# - Part 4 | daily.dev
og:description: Part four of a series implementing a .NET garbage collector in C# covers how to walk the managed heap by computing object sizes from method table metadata, and...
og:url: https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq
og:image: https://api.daily.dev/og/posts/0VwTMVEFq.png
og:image:alt: Writing a .NET Garbage Collector in C# - Part 4
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.

# Writing a .NET Garbage Collector in C# - Part 4

**[Kevin Gosse \(minidump.net\)](https://daily.dev/sources/minidump)** · 15 min read · 0 upvotes · 0 comments

## Summary

Part four of a series implementing a .NET garbage collector in C# covers how to walk the managed heap by computing object sizes from method table metadata, and how to manage allocation contexts. It then introduces segments as a more efficient alternative to tracking individual allocation contexts, explaining how to allocate dummy 'free object' placeholders to keep segments walkable, and how to use IGCToCLR.GcEnumAllocContexts to fix up allocation contexts still in use during garbage collection. Code for the article is available on GitHub.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://minidump.net/writing-a-net-gc-in-c-part-4>

## Questions this post answers

### How do you compute the size of a .NET object from its method table to walk the managed heap?

For fixed-size objects, read the BaseSize field of the method table directly. For variable-size objects like arrays and strings, read the length (stored right after the method table pointer), multiply it by ComponentSize from the method table, and add that to BaseSize to get the total object size.

_Developers digging into .NET internals can keep up with deep technical series like this on daily.dev._

### Why does the .NET garbage collector use segments instead of tracking individual allocation contexts?

Segments let the GC balance CPU overhead against memory usage: allocation contexts are kept small (around 8KB) to minimize wasted memory, while new contexts are cheaply carved out of a larger preallocated chunk called a segment, avoiding the cost of allocating memory from the OS on every request. Gaps left by context boundaries are filled with dummy 'free object' placeholders so the heap stays walkable.

_Anyone weighing GC design trade-offs can track deeper explainers like this through daily.dev._

## Similar posts on daily.dev

- [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
- [The Fasting Developer: Memory Discipline and GC Awareness in .NET](https://daily.dev/posts/the-fasting-developer-memory-discipline-and-gc-awareness-in-net-5yq85h552) · We Are .NET · 12 upvotes · 0 comments
- [Win16 Memory Management](https://daily.dev/posts/win16-memory-management-iurgwfefx) · Lobsters · 0 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq)

```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":"TechArticle","headline":"Writing a .NET Garbage Collector in C# - Part 4","url":"https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq"},"datePublished":"2026-10-04T13:06:40.439Z","dateModified":"2026-10-04T13:13:59.058Z","description":"Part four of a series implementing a .NET garbage collector in C# covers how to walk the managed heap by computing object sizes from method table metadata, and...","image":"https://media.daily.dev/image/upload/s--CxzD6vbw--/f_auto/v1722860399/public/Placeholder%2005","thumbnailUrl":"https://media.daily.dev/image/upload/s--CxzD6vbw--/f_auto/v1722860399/public/Placeholder%2005","isAccessibleForFree":true,"articleSection":"Kevin Gosse (minidump.net)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kevin Gosse (minidump.net)","logo":"https://media.daily.dev/image/upload/s--8KHn1aFB--/f_auto,q_auto/v1791119046/logos/minidump?_a=BAMAMicg0","url":"https://daily.dev/sources/minidump"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#","timeRequired":"PT15M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kevin Gosse (minidump.net)","item":"https://daily.dev/sources/minidump"},{"@type":"ListItem","position":3,"name":"Writing a .NET Garbage Collector in C# - Part 4"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/writing-a-net-garbage-collector-in-c---part-4-0vwtmvefq#faq","mainEntity":[{"@type":"Question","name":"How do you compute the size of a .NET object from its method table to walk the managed heap?","acceptedAnswer":{"@type":"Answer","text":"For fixed-size objects, read the BaseSize field of the method table directly. For variable-size objects like arrays and strings, read the length (stored right after the method table pointer), multiply it by ComponentSize from the method table, and add that to BaseSize to get the total object size. Developers digging into .NET internals can keep up with deep technical series like this on daily.dev."}},{"@type":"Question","name":"Why does the .NET garbage collector use segments instead of tracking individual allocation contexts?","acceptedAnswer":{"@type":"Answer","text":"Segments let the GC balance CPU overhead against memory usage: allocation contexts are kept small (around 8KB) to minimize wasted memory, while new contexts are cheaply carved out of a larger preallocated chunk called a segment, avoiding the cost of allocating memory from the OS on every request. Gaps left by context boundaries are filled with dummy 'free object' placeholders so the heap stays walkable. Anyone weighing GC design trade-offs can track deeper explainers like this through daily.dev."}}]}
```

