RE: gc.KeepAlive - is this the reality of garbage collection
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 27 Mar 2007 19:24:11 +0100
Jeffrey Tan[MSFT] <jetan@xxxxxxxxxxxxxxxxxxxx> wrote:
Besides other community member's replies, below is my comment:
You only need to use GC.KeepAlive() method while dealing with unmanaged
resource/code.
I'd put it as only needing it while implementing a finalizer.
If you are using pure managed code without unmanaged
interop, there is no need to keep the managed object alive. Actually, if
you do not deal with unmanaged resource, there is no need to implement
IDispose interface or Finalizer.
Well, hang on. There are other reasons for implementing IDisposable
than merely collecting unmanaged resources, and while I wouldn't
implement a finalizer without *directly* having access to unmanaged
resources, it's still good practice to implement IDisposable if you
have a reference to something else which implements IDisposable (eg a
stream). In that case you don't directly have access to the unmanaged
resource, but you still want to be able to ask the managed type to
clean up the unmanaged resource in a timely manner.
While dealing with unmanged resource/code, you only need to use
GC.KeepAlive if the unmanaged code is executed asynchronously with your
managed code(such as in background thread).
Well, the finalizer thread is always a separate thread anyway, so that
doesn't really cut the scenarios down.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- RE: gc.KeepAlive - is this the reality of garbage collection
- From: "Jeffrey Tan[MSFT]"
- RE: gc.KeepAlive - is this the reality of garbage collection
- References:
- gc.KeepAlive - is this the reality of garbage collection
- From: Rolf Welskes
- RE: gc.KeepAlive - is this the reality of garbage collection
- From: "Jeffrey Tan[MSFT]"
- gc.KeepAlive - is this the reality of garbage collection
- Prev by Date: Re: RegEx and Vb.net /// "Unrecognized escape sequence"
- Next by Date: Re: Console application will only run on machine where application was compiled
- Previous by thread: RE: gc.KeepAlive - is this the reality of garbage collection
- Next by thread: RE: gc.KeepAlive - is this the reality of garbage collection
- Index(es):
Relevant Pages
|