Showing posts with label Garbage Collector. Show all posts
Showing posts with label Garbage Collector. Show all posts

Monday, September 10, 2012

GC - The Garbage Collector in managed .Net world

If you in the managed program execution world, you cannot avoid the word GC even though everybody says its managed automatically and there is no need to worry about memory management during coding. Ideally the garbage collection ie memory management should be done intelligently by the managed runtime environment otherwise there is no need to adopt that run-time. But the truth is opposite. Earlier when we had managed the memory ourselves, it was easy as we were deleting the objects .But now we need to depend and learn the working of another software piece written by somebody else to effectively manage memory of our application and that is called GC ie the GarbageCollector.Not a joke. If you don't know the working of GC you cannot survive in the managed world .At least it will be a bottle neck in your job interview.

This post is not intended to explain about the GC in paragraphs. This helps to refresh the concepts about GC by just pointing to already written articles. This means you need to have prior knowledge about GC.

What .Net GC does?

  • Collects unused objects and frees memory
  • Compacts / Defragment the memory of Gen 0,1,2 after clearing the speace used by the objects.
  • There is LOH (Large Object Heap) which contains large objects .Normally size > 85KB. This space is not defragmented by GC. Why there is a LOH is because the object moving process is heavy and may impact the performance.
  • LOH objects are marked as deleted by GC and GC keeps track of free space in LOH to perform allocation for the next time.

How GC works

  • Use generational algorithm.
  • Start by assuming that all the objects are garbage except the application roots.
  • Application root
    • Any static variable is application root
    • Any local variable or method params of function which is executing when the GC starts.
    • Any object in heap which is pointed by any CPU register.
    • Any object whose pointer is present in freachable queue or passed to COM+ via interop.
  • It creates memory map by traversing the objects starting from application roots.
  • If the object is in use it promotes the object from its current generation to higher generation.
  • Generation is simply an area in the memory heap. There are 3 Generations
  • Gen 0
    • Contains all the newly created objects.
  • Gen 1 
    • Contains objects which are promoted by GC from gen 0 .ie the objects which are still in use.
  • Gen 2
    • Contains objects which are promoted from Gen 2 .Mainly old objects.
  • Once the collection completes GC performs a compaction in the generation.
  • After compaction it moves the next object pointer to the end of allocated objects so that next time the new keyword can execute faster.
  • Collecting unwanted objects from these 3 generations is time consuming.So it can perform partial collections say collect only Gen 0.
  • Once the object seems Garbage to GC it moves the object pointer from Finalization queue to the freachable queue in case the object pointer is present in the finalization queue.
    • The entry to the finalization queue is put at the time of object allocation (new keyword) if the Finalize method is overriden
  • When any object pointer is available in the f-reachable queue the finalization thread starts and calls the Finalize method on each object. At this time the object becomes non-garbage.
  • Next time when GC finds the finalized objects as garbage it simply removed the objects.

Finalize method 

  • This is a mechanism to object to cleanup themselvs
  • Similar to destructor and now C# allows us to write the Finalize in the destructor syntax only.
  • This destructor method will be converted to Finalize method during compilation which means there is no real destructor in C# .Net.
  • GC calls this Finalize method after marking the object as garbage.

When GC starts collecting garbage?

  • It is NOT based on time interval.
  • When somebody calls the GC.Collect
  • When there is not enough physical memory
  • When a generation (heap) is full so that it cannot allocate new objects or move survived objects to next generation.

Monitoring GC

External links

Tuesday, September 27, 2011

How to know when .Net GC is working

One of my previous posts I was explaining how to learn the working of JIT during an application run. That gives us actual feeling of how the concept if working in the real world. If you are interested to know how the other concepts are working you can monitor using the windows tool called perfmon.

Perfmon.exe can be found inside the system32 folder in Windows installation

<InstallDrive>:\Windows\System32\perfmon.exe

You can easily open the application by just typing perfmon in the search box of start menu.

Using Perfmon to monitor the .net Activities

Perfmon is a great tool and its not possible to cover all the aspects of it in this post. Just google for its full usage scenarios. I am just concentrating on how to use it to monitor when GC is working for our application. You can use the same method to know when the jitting happens.

Ok. Lets open the perfmon and watch what are the options in it.There are PerformanceMonitor, ReliabilityMonitor, Data collector sets and Reports.We are interested in the performance monitor where we get the status of GC using a counter.What is a counter in perfmon?Counter is a criteria or specification what to monitor which can be added to a graph to see its current value. By default the processor time counter will be available there. Below are the easy steps to add a counter for monitoring GC of an application.

Goto Performance monitor->Click ”+” button at top –>Expand “.Net CLR Memory” in the available counters->Select “# Gen 0 Collections”->Select your application in the “Instances of selected object” section->Click on “Add” –>Click “Ok”

Remove the processor time counter for easy monitoring. Now the graph will reflect the change when GC comes into picture. For learning purpose better put a button to invoke GC using GC.Collect method .So that you can see the counter increments when you click on that button.

When you add the counter you might have noticed so many counters available there .Play with those and conquer the technology with its inner working.

Tuesday, January 4, 2011

Loading SOS dll from Visualstudio for debugging

The Windbg is really an amazing debugging tool.With sos.dll we can even realize the working of garbage collector through windbg. Earlier my understanding was ,I need to use Windbg to use sos.dll for debugging.Now I realized that we can even load the sos.dll from immediate window of visual studio while debugging. Its very cool.One tool for everything...

Below are some links which explains how to load sos debugger from VSTS immediate window and see the memory details.

http://blog.mohammadjalloul.com/blogs/mo/archive/2010/02/21/the-large-object-heap.aspx
http://blogs.msdn.com/b/vijaysk/archive/2007/11/15/sos-your-visual-studio.aspx
http://msdn.microsoft.com/en-us/library/yy6d2sxs%28v=VS.100%29.aspx

Note : The Express edition of Visual studio doesn’t support this sos debugging.