Re: garbage collection of open resources
From: mikeb (mailbox.google_at_nospam.mailnull.com)
Date: 04/22/04
- Next message: msnews.microsoft.com: "About C#"
- Previous message: John Wood: "Re: DrawImage draws faded image"
- In reply to: David Browne: "Re: garbage collection of open resources"
- Next in thread: Mark Broadbent: "Re: garbage collection of open resources"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 22 Apr 2004 11:04:33 -0700
David Browne wrote:
> "John Wood" <j@ro.com> wrote in message
> news:%23W69rkIKEHA.1340@TK2MSFTNGP12.phx.gbl...
>
>>That's actually very interesting -- you'd think that the CLR would ask the
>>GC to finalize everything before it terminates... that's kind-of a bug
>
> don't
>
>>you think?
>>
>
>
> It does, there's just no guarantee. If finalizers take too long or create
> too many new objects the CLR will give up and shut down the process without
> finalizing all the objects.
>
While not being guaranteed is one thing, it actually *appears* to make
little or no effort. Maybe there's something in the sample that would
cause the finalization of the StreamWriter (or some other object that I
don' know about) to take too long, but I don't know what it is and if
there's any guidance about it I'd appreciate a pointer.
In practice, this means that you'd better make sure your code calls
Close() or Dispose() (either directly or via a 'using' statement) on
buffered file objects (and other resources, too, I'm sure). The
fallback that the GC will eventually take care of Disposing objects that
your code didn't handle explicitly does not seem to hold for a
significant situation (clean process termination).
Clearly, not calling Dispose() on a disposable object is a bug in the
application; however, I just wanted to point out that resource leakage
while the app is running is not the only side-effect of neglecting
Dispose() - data could be lost as well.
-- mikeb
- Next message: msnews.microsoft.com: "About C#"
- Previous message: John Wood: "Re: DrawImage draws faded image"
- In reply to: David Browne: "Re: garbage collection of open resources"
- Next in thread: Mark Broadbent: "Re: garbage collection of open resources"
- Messages sorted by: [ date ] [ thread ]