Garbage Collection in .Net Part-1

Ramkumar Radhakrishnan
2 min readSep 30, 2018

--

Garbage collection is not any process triggered by any means manually or through any program its just like any other process initiated by .Net framework on its own by making few decisions and the decision is nothing but when a program started it is initiated automatically in background. Good isn't it.

What is the story then?

when ever you create any instance in C#, CLR allocates memory in heap and this process is repetitive for all the objects you created, but the memory allocation is not unlimited means to say its not like you can use the whole 16gb RAM for your program,so it necessary to manage the allocated memory.

Garbage Collector comes into rescue for this action. It just make a trip to Heap and check what are unused memory at current state also checks if needed with upcoming operations and if not your dead now and free up the allocated memory. This is the high level picture.

OK, now F12(if haven't used VS sry dude your supposed to close the article here), means one step inside,

When ever a process triggered a virtual memory created from the main memory for the process. Thus the program deals only with the allocated virtual memory not the main. GC also works mainly on this virtual.

OK Now F12 on Virtual Memory,

Virtual Memory has three blocks

  1. Free (no definition required)
  2. Reserved (already allocated)
  3. Committed(This memory not given out to main/physical and not available for space allocation).

Now an example,

Have you ever faced memory error due to virtual memory full ? Kind of exception faced at most of enterprise applications.

Now you should relate to the above said 3 points and GC comes into action to rescue from the run time error.

--

--

Ramkumar Radhakrishnan

C# for Life. Python and Flutter are added to make my life for more flavors. Acting as Tech Lead at IR. But not leading a team literally, since nobody cares me