RE: gc.KeepAlive - is this the reality of garbage collection

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
.



Relevant Pages

  • Re: " //Clean Up managed resources " f&*ck
    ... Since Dispose ... methods is responsible for cleaning resources, the finalizer code ... it is about 'managed resource'. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: finalize() overhead
    ... of explicit resource clean up. ... Do you trust the finalizer to release locks for you ... code to be in a sane state if a forgotten lock is suddenly ...
    (comp.lang.java.programmer)
  • Re: OutOfMemoryException
    ... The CLR uses the large object heap for all allocations greater than some ... These will ensure that your resource is freed no matter how you ... and doesn't rely on the finalizer thread to pick up the ...
    (microsoft.public.dotnet.framework.clr)
  • Re: " //Clean Up managed resources " f&*ck
    ... So this leaves the difference between a Managed Resource and a Unmanaged resource. ... These resources will not be closed ot cleaned up by the Garbage Collector, unless the class holding on to the resources implements at least a Finalizer. ... Now on the issue of why we need a dispose function *and* a finalizer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: " //Clean Up managed resources " f&*ck
    ... So this leaves the difference between a Managed Resource and a Unmanaged ... be cleaned up by the Garbage Collector. ... Now on the issue of why we need a dispose function *and* a finalizer. ...
    (microsoft.public.dotnet.languages.csharp)