Last post Apr 14, 2015 02:48 AM by Paul Linton
Apr 13, 2015 05:54 AM|Zaara|LINK
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.
public B bobj;
public A(B obj)
bobj = obj;
public string name ="B name";
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.
Apr 13, 2015 10:32 AM|gerrylowry|LINK
@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
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.
Apr 13, 2015 07:25 PM|JohnBert|LINK
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
Apr 14, 2015 02:48 AM|Paul Linton|LINK
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 more) 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.)