Tag Archives: Garbage Collection

Garbage Collection in C#

Garbage Collection is clearing up more memory, by getting rid of useless objects that live on the heap. But how do we determine which objects are deemed “useless” and which ones aren’t?
This part is simple, if there is a “live reference” to the object on the heap, it should be kept alive. As long as there is a way of leading to the object, you can’t get rid of it. Doing this has the potential (and most likely will) destroy your application.

Imagen that you rely on some object named “Bank”. But, because of a faulty garbage collection, the Bank object is removed from the heap, and so is all the values it held. This could cause many bugs.

Whether an object should or should not be collected is done in different steps, checking everything on the heap would be a HUGE task and the memory required to do so, is about twice as high as the memory your application is using. (Though this might sound like a bad thing, GC is of great importance). The first step is checking which objects have a reference, the GC goes past all objects (remembering where he has been, we don’t want an infinite loop) and when he sees that there is no connection to the object anymore, he will mark it as “waste”.

After he went over them, at the point when the second step (actual GC) kicks in, he will either promote the objects, or remove them. There are 3 levels of promotion an object can get (Actually, there is a 4th one that is kind of special, which I’ll cover later). We call them respectively Gen0, Gen1 and Gen2.

After Gen0 gets full, the GC will kick in and when an object survives the first pass, he will get promoted to Gen1. Because most of the objects on the heap aren’t used for such a long time, most of them will get removed by the GC at this first Generation. So this causes Gen1 to not get full as quickly as Gen0 does. By doing this, he already managed to divide the heap into a section “Gen0” where he will have to remove objects quite frequently, and a Gen1 that is less likely to get full. Because Gen1 won’t get filled as quickly as Gen0, there is less GC required here. This saves the GC quite a bit of work, which is a good thing because as mentioned before, it is a very stressful task for your computer.

Same goes for Gen2, if Gen1 is full, it will have to do garbage collection on that section, and the surviving objects get promoted to Gen2. But what happens if Gen2 is full and there can’t be any space freed by the garbage collector? Your program will throw an “OutOfMemoryException”. Ending your program immediately.



As mentioned before, there is indeed a 4th section. This one is called the “Large Object Heap”, this one provides storage for objects of size “ > 85000 bytes”. These are deemed too expensive to move around all the time from one Gen to another, so they are just stuck in here.


When moving objects from one section to another, you have to keep in mind that the location changes. So when there is a reference to that particular object, the reference has to be to the new address. This is another task complicating the work of the GC, it has to make sure that every reference has a matching point on the heap. Otherwise GC would ruin your application.


One of the other advantages of automatic GC, is that when there is freed up space, it gets allocated next to eachother. To get why this is valuable, look at the following image of Native GC.


The grey area is the memory that the GC freed, this is a total of 12bytes. Though there are living objects between “A” and “D E”. This means that if I’d want to allocate a new object of 10bytes, I wouldn’t be able to because it wouldn’t fit anywhere. Though while in some language it might still be like this, in C# this is not the case, because the GC in C# makes sure that when he has free space, he will put it together as one big block of free space. Let’s look at this very same image in C#.



If I’d want to allocate an object of 10b now, It would perfectly fit in the block of 12b.

So to come to a conclusion; While the garbage collector is actually a complex thing, and does require quite a bit of calculation power from your computer, it is a very valuable thing. It saves us programmers a lot of work trying to figure out how we can manage the memory ourselves.

Garbage collection

Now some of you familiar with this movie might think, how is this related to anything in my computer and how the  fudge did he get there? Very simple, I'm talking about his job which is garbage collecting. The garbage collector in your computer is probably something what many programmers praise to, as it's made their lives just that bit nicer. Before GC (Garbage colletion) programmers had to do manual memory management. What it does can be explained quite easy, yet the whole process of doing so might just be a little harder to understand. What GC does, it finds data objects in a program that aren't of any use anymore, after it found those objects it will tag them, meaning that they can be overwritten with new data. Advantages of this way of memory management are obvious, without the need of manual memory management the chances of making mistakes are significantly reduced. With manual GC you might try to free space that has been freed before and possibly been assigned again, resulting in bugs in your program.

How does the GC know what fine memory units to take with him? In the coding it knows when an object is in use, not being used at the moment, or never to be used again.

Dim x as string
Dim y as string
dim x as double
y = "Please don't take me"


In this example we can safely state that after x became a 'double', there was no need  for the String anymore. x has been overwritten if you want. But the y string is still in use later in the program so it's best to keep both of these in the memory.

So how do programming languages know about the GC? Most of the higher-level programming languages are likely to have it built-in as a standard feature (Fortran, C#, Pascal, etc..) and even if some languages don't have it built-in, it can be added trough libraries.

This is quite a simple view of what garbage collection is, why we have it and love it, and how it works.