As of my knowledge i know like GC performs collection operation in order to remove unmanaged resources to release memory which is called implicit clean up. And by using 'USING' keyword we can do it explicit cleaning but my doubt is how GC releases managed resources.
-
What do you mean by "how"? Take a look the [MSDN documentation](http://msdn.microsoft.com/en-us/library/ee787088.aspx#conditions_for_a_garbage_collection) and let us know if you have any specific questions. – Eren Ersönmez Jun 15 '12 at 06:47
-
Thanks for posting, how means like finalize method for unmanaged is there any method is there for managed sources. – nag Jun 15 '12 at 06:49
-
Hey Eren can you please post your answer then I'll mark it as answer. – nag Jun 15 '12 at 06:52
-
Hi Botz:Finalize method is to release for unamanged resources implicitly right? or wrong? – nag Jun 15 '12 at 06:54
-
1@nag - Finalize in C# is called by the GC at the point it's going to remove the object, it's a last chance to clean up any unmanaged resources the object might have open before they get orphaned. Generally you should implement the dispose pattern to allow you to clean them up as soon as you're done with them via the 'using' keyword – Paolo Jun 15 '12 at 06:58
-
2@nag Sorry, I confused that part myself. Yes, you can release unmanaged resources in Finalize(). But you should only use it as a backup, because you never know when it is called. better implement IDisposable and clean up unmanaged resources in Dispose(). – Botz3000 Jun 15 '12 at 06:59
-
@paolo so its no matter its manged or unmanged resource when it calls finalize method? – nag Jun 15 '12 at 07:02
-
so GC calls Finalize implicitly at some time to release unmanaged resources, and can any one tell me its mandatory to use GC.SupressFinalize method after dispose if so why? – nag Jun 15 '12 at 07:06
-
Thanks all of you guys, for your valuable answers. :) – nag Jun 15 '12 at 07:19
-
@nag - The GC can only call finalize on managed objects, it has not idea about unmanaged things (which is why you have to release them yourself) – Paolo Jun 15 '12 at 09:10
1 Answers
You don't have to do anything special in order for GC to clean up your managed resources. GC will clean it up sometime after there are no references left to your managed resource.
If your managed resource owns unmanaged resources, you can implement the IDisposable interface and call the Dispose
method, in which you'd explicitly clean up your unmanaged resources. using
statement makes it very easy to use this interface, as it automatically calls Dispose when the code exists the using
block, even in case of exceptions.
You might take a look at the MSDN documentation on Garbage Collection.
EDIT: based on comments above.
You could override Object.Finalize
by defining a finalizer (e.g. ~MyClass()) but there is no telling when it will be called by GC. IDisposable is generally preferred. More info on Finalizer vs Dispose here.

- 1
- 1

- 38,383
- 7
- 71
- 92
-
Additionally, if something goes wrong in your implementation of finalize() and an unhandled exception is thrown, the garbage collector will crash, and you _will_ have a bad time debugging it :) – cwap Jun 15 '12 at 07:18
-
1@cwap If I can remember correctly, you can catch those if you subscribe to the AppDomain.UnhandledException event. – Eren Ersönmez Jun 15 '12 at 07:22
-
1Could be. Would make sense I guess, gonna have to try it next time someone decides to mess with Finalize() :P – cwap Jun 15 '12 at 08:23