Find Related products on Amazon

Shop on Amazon

Writing a .NET Garbage Collector in C# – Part 1

Published on: 2025-07-12 02:28:40

If you read my articles, you probably know that I like playing with NativeAOT a lot, especially to use C# in places where it wasn’t possible before. I already wrote a simple profiler, this time we will go a step further and try to write a Garbage Collector in C#. Of course, this won’t result in anything usable in production. Building a performant and fully-featured GC would take hundreds of hours of work, and using a managed language for that is a poor choice (can you imagine your GC being randomly interrupted by its own internal garbage collection?). Still, this is a good excuse to learn more about the internals of the .NET Garbage Collector. Also, you may be wondering why NativeAOT is needed at all for this. Like for the profiler in .NET, there are two reasons why using “vanilla” .NET is impossible: first, the GC is initialized very early in the process, at a point in time when the CLR isn’t ready to run managed code yet. Second, the GC would end up depending on itself, which is a ... Read full article.