The text of this article is not in this database — only its details are. Read it on the old site: Back To Basics - Dispose Vs Finalize
20 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: Back To Basics - Dispose Vs Finalize
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
Junaid AnsariPosted Feb 6, 2022, 7:31 AM
You had me at Now question is, When to implement Finalize? There may be any unmanaged resource for example file stream declared at class level. We may not be knowing what stage or which step should be appropriate to close the file. This object is being use at many places in the application. So in this scenario Finalize can be appropriate location where unmanaged resource can be released. It means, clean the memory acquired by the unmanaged resource as soon as object is inaccessible to application.
Sasi RekhaPosted Dec 18, 2018, 6:28 AM
I was searching for what happens if declare dispose() in destructor ,finally i got answer,its really great
Siddhant ShivaniPosted May 27, 2018, 4:24 AM
Here, The reason given for finalize to be expensive is "it just clean the memory that is used by unmanaged resource. Memory used by managed resource still in heap as inaccessible reference.". Actually we don't clear managed resource from finalize because it will automatically be removed by the GC because when finalize method is called all the references to the object are out of scope. So, it is not recommended to remove the managed resources manually. Otherwise, From Dispose and finalize both we call the same Dispose(bool disposing) method. It is on us whether we want to clear the managed resource from finalized metod or not.But, It is not recommended to clear the managed resource from finalize. You can check the implementation of these methods in any website or book. The reason for finalize to be expensive is it waits till all the reference of object goes out of scope then only it cleans the memory. But for the dispose method it is in our control when we want to release the resources. we can call the dispose method as early as possible. It is always recommended to implement finalize method as well along with dispose If in any case we forgot to call the dispose method. Then finalize method saves us from memory leak.
kalu singh raoPosted Jul 8, 2016, 1:21 AM
Nice...
AmalaratchagarPosted Jun 14, 2016, 11:29 AM
Good to read these kind of basics articles again after many years..
Pankaj Kumar ChoudharyPosted May 20, 2016, 5:09 AM
Nice Explanation.........
Manoranjan BeheraPosted Apr 29, 2016, 8:07 AM
So nice decription
Shridhar SharmaPosted Oct 16, 2015, 1:01 PM
nice share
Kamlesh BhorPosted Oct 16, 2015, 8:10 AM
Nice...
Humayun Kabir MamunPosted Oct 16, 2015, 8:04 AM
Nice...
Nilesh JadavPosted Oct 16, 2015, 7:20 AM
Good Explain
Mukesh KumarPosted Oct 16, 2015, 7:04 AM
I was so confuse between both ... but now i am clear my doubts thanks
Rajeesh MenothPosted Oct 16, 2015, 6:49 AM
Nice One!
Harshad PansuriyaPosted Oct 16, 2015, 6:30 AM
Nice one
Aarya MorchePosted Aug 20, 2013, 11:39 PM
one question ..... Garbage collector maintains a separate queue when it adds all object which has finalized implemented , but if those objects having some internal chain of references then still it is clean the memory for those objects?
Nitya SharmaPosted Mar 28, 2013, 3:38 PM
when object becomes out of scope or object no longer in use, memory occupied by that object doesn't get released immediately. It gets release whenever GC runs. We can't control when GC runs, it runs with some interval or whenever more memory requires by application.
udit guptaeditedPosted Jul 26, 2012, 11:57 PMEdited Jul 27, 2012, 12:00 AM
Hey one question.. since finalize is implemented in C# using deconstructer and we can create one of our own which gets called soon after the object is no longer in use, so does that mean we are accessing finalize()? actually this is in regards to the statement that we can not control when finalize gets called
udit guptaPosted Jul 26, 2012, 11:57 PM
Nice Article
RaananPosted Nov 25, 2011, 4:39 PM
:)
Dinesh BeniwaleditedPosted Nov 17, 2011, 3:12 AMEdited Nov 17, 2011, 3:12 AM
Good start, Welcome to the Mindcracker Community Nitya.