locked
Destructor in c# RRS feed

  • Question

  • User-630248527 posted

    Hi,

    It regarding destructor behavior in c#. I have many reference to same instance in multiple class. How do the destructor delete the memory it? Below is the example snippet.

    class A
    
    {
    
    public B bobj;
    
    public A(B obj)
    
    {
    
    bobj = obj;
    
    }
    
    }
    
    class B
    
    {
    
    public string name ="B name";
    
    public B()
    
    {
    
    }
    
    }
    
    class Main
    {
    B bobj;
    A aobj;
    bobj = new B();
    aobj = new A(bobj);
    
    }
    
    }
    


    When does the bobj from class Main  gets destroyed? Since aobj.bobj also refer to the same object? How does the destructor handles it?

    I hope GC handle based on reference to an object? Is there any chance when aobj.bobj still requires to be alive but bobj in main gets destroyed. so in tuen reference of aobj.bobj is not there.

    Thanks,

    Zaara.

    Monday, April 13, 2015 5:54 AM

Answers

  • User-434868552 posted

    @Zaara    welcome to forums.asp.net

    you need to read about garbage collection and dispose.  MSDN is one of your best friends.

    for garbage collection, search      msdn c# garbage collection 

    Garbage Collection https://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx 

    When the garbage collector performs a collection,
    it checks for objects in the managed heap that are
    no longer being used by the application and
    performs the necessary operations to reclaim their memory.

    for Dispose, search          msdn c# dispose 

    from "Implementing a Dispose Method" https://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx 

    The dispose pattern is used only for objects that access unmanaged resources,
    such as file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory.

    ... the garbage collector is very efficient at reclaiming unused managed objects,
       but it is unable to reclaim unmanaged objects.

    Zaara, for most c# programs that you will write in the next few years, it's unlikely that you'll need to worry about garbage collection.

    i've been using c# as my main language since 2007 and garbage collection has never been an issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2015 10:32 AM

All replies

  • User-434868552 posted

    @Zaara    welcome to forums.asp.net

    you need to read about garbage collection and dispose.  MSDN is one of your best friends.

    for garbage collection, search      msdn c# garbage collection 

    Garbage Collection https://msdn.microsoft.com/en-us/library/0xy59wtx(v=vs.110).aspx 

    When the garbage collector performs a collection,
    it checks for objects in the managed heap that are
    no longer being used by the application and
    performs the necessary operations to reclaim their memory.

    for Dispose, search          msdn c# dispose 

    from "Implementing a Dispose Method" https://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx 

    The dispose pattern is used only for objects that access unmanaged resources,
    such as file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory.

    ... the garbage collector is very efficient at reclaiming unused managed objects,
       but it is unable to reclaim unmanaged objects.

    Zaara, for most c# programs that you will write in the next few years, it's unlikely that you'll need to worry about garbage collection.

    i've been using c# as my main language since 2007 and garbage collection has never been an issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2015 10:32 AM
  • User-1078840699 posted

    Hello,

    The C# language calls these "destructors", but most people call them "finalizers" since that is their .NET name and it reduces confusion with C++ destructors (which are quite different).

    How to Implement IDisposable and Finalizers: 3 Easy Rules

    For more information do check this link 

    Monday, April 13, 2015 7:25 PM
  • User-1910946339 posted

    I hope GC handle based on reference to an object?

    Are you wondering if Garbage Collection works properly in Windows?  Do you have any serious reason to think that it would not work properly?

    Is there any chance when aobj.bobj still requires to be alive but bobj in main gets destroyed. so in tuen reference of aobj.bobj is not there.

    There is no 'chance' involved.

    What is the reason for your questions?  It appears that you have not looked at how the .Net Framework does garbage collection but that you think that Microsoft got it wrong and that the majority of all developers in the world have not noticed. There must be a bazillion (or maybe moreSmile) articles on how the garbage collector works.  Not to mention that all the source code is available for you to read yourself.

    (But seriously, don't waste your time.  Of course the garbage collector works.)

    Tuesday, April 14, 2015 2:48 AM