You must Sign In to post a response.
  • Category: .NET

    Doubts regarding Garbage Collection in C#

    While going through c# garbage collection I came across a lot of terms like Managed/unmanaged resources, Idisposable, Finalize and I must say that things felt seemingly understandable. However I have a few queries:

    1) Are all classes contained by System.Data are managed and all others are unmanaged resources?

    2) What is this garbage collector anyway. Is it a part of the CLR? how does it run or rather who calls it and when?

    3) While debugging using Visual studio IDE can a programmer at any time check the objects that persists in memory or the ones that has been disposed/collected by GC?


    Thanks
  • #764699
    Hai Tarun,
    Below are the answers as per my understanding:
    1) Are all classes contained by System.Data are managed and all others are unmanaged resources?
    Ans. Yes, the objects which comes under the .net framework are managed objects. So these objects will be claimed by the Garbage collector. But you need to make sure that what these objects are doing like Creating and opening connection using SqlClient. As the SqlClient is .Net object but it is doing the things for external connection (with sql server), so we need to explicitly dispose it.
    2) What is this garbage collector anyway. Is it a part of the CLR? how does it run or rather who calls it and when?
    Ans. It is a background process or we can say utility which runs in background and reclaim the objects to dispose. It automatically gets called when the heap memory is full with the objects present.
    3) While debugging using Visual studio IDE can a programmer at any time check the objects that persists in memory or the ones that has been disposed/collected by GC?
    Ans. There are the tools like Ants Memory Profiler which can be used to track the memory occupied by the class objects.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764711
    1) Are all classes contained by System.Data are managed and all others are unmanaged resources?
    Ans. The code which you are handling in your application is managed code. The components(3r Part, COM etc..) which you are using but you can not handle its code will come under unmanaged.

    2) What is this garbage collector anyway. Is it a part of the CLR? how does it run or rather who calls it and when?
    It is very good memory management system handly by CLR in .net framework. There is machanism when and how to call GC. CLR will handle it. If you want to call GC in your code that also you can handle.
    For ex. If you do not want to dispose any of the object while GC collect, you can handle it.

    3) While debugging using Visual studio IDE can a programmer at any time check the objects that persists in memory or the ones that has been disposed/collected by GC?
    Yes you can use tools. There are lot of tools available to see the object at the run time

    By Nathan
    Direction is important than speed

  • #764736
    1. The code in your application is managed code and any code which is referred as dll or third party component is unmanaged coded.

    2. Best place to refer to know more about GC is MSDN,
    https://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx

    3. You cannot see the object disposal using VS IDE but as mentioned above there are many tools to monitor this activity.


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

  • #764782
    Look, the classes who handle stream, live data, audio/video, com, interop, third party dlls are mainly unmanaged resource and does not collect by GC. There is no specific classes/namespaces that contain or represent managed/unmanaged resources. Open files, Open network connections, Unmanaged memory, In XNA: vertex buffers, index buffers, textures, etc. Normally you want to release those unmanaged resources before you lose all the references you have to the object managing them. You do this by calling Dispose on that object, or (in C#) using the using statement which will handle calling Dispose for you
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments