Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

Premature .NET garbage collection, or "Dude, where's my FooBar?"

, 29 Aug 2011
Rate this:
Please Sign up or sign in to vote.
It would be very nice if one could ask the .NET compilers to automatically add GC.KeepAlive(this) to the end of a class' methods; IMHO, that should have been a default behavior for classes that override Finalize. Unfortunately, since that isn't possible, one should probably, as a matter of...

It would be very nice if one could ask the .NET compilers to automatically add GC.KeepAlive(this) to the end of a class' methods; IMHO, that should have been a default behavior for classes that override Finalize. Unfortunately, since that isn't possible, one should probably, as a matter of course, include GC.KeepAlive(this) to the end of any class method which overrides Finalize.

I find it in a way curious that the creators of .NET seemed to expect Finalize to be the primary means of cleaning up after objects are no longer needed, while Dispose was something of an afterthought. While there are some scenarios where proper use of Dispose would be difficult or impractical, there are so many cases where Finalize really isn't sufficient, and so many more cases where it can lead to hard-to-track bugs, that I would in many cases think it better for a class to require the use of Dispose for correct operation, than for it to attempt cleanup using Finalize (using Finalize to sound an alarm if a class isn't disposed may be a good idea, though). Among other things, if a class never attempts cleanup via Finalize, failure to Dispose will often yield consequences which are highly visible, and thus are likely to be diagnosed and fixed. By contrast, if a class overrides Finalize to perform silent cleanup in case of a missing Dispose, it's more likely that a missing Dispose will go unnoticed, and there's a significant chance that a missing KeepAlive will cause random failures. Note that a missing KeepAlive will generally not cause trouble if the user of a class properly Disposes it, since the compiler will have to keep the object around at least until the Dispose call.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

supercat9
Web Developer
Unknown
Embedded systems programmer since 1994.

You may also be interested in...

Comments and Discussions

 
GeneralI agree that it is curious that the KeepAlive call is necess... PinmemberPhil Atkin29-Aug-11 0:25 
GeneralRe: KeepAlive can be required even in 100% managed code, if a co... Pinmembersupercat929-Aug-11 11:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 29 Aug 2011
Article Copyright 2011 by supercat9
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid