Re: GC and Dispose method questions

From: Teresa (Teresa_at_discussions.microsoft.com)
Date: 01/14/05


Date: Thu, 13 Jan 2005 18:59:07 -0800

Thanks for everyone help!!!! I'm new to .NET and have a very tight deadline
on a .NET proj, I really appreciate all your quick responses.

I have two follow up questions...

1) If my class contains only string and integer members (no DB connection or
file or unmanaged objects), is it still good practice to have a dispose
method to set these member variables to nothing (for string) and 0 (for
integer)?

2) In AMoose's example, if I were to call the dispose (close) method for the
IO.FileStream object, F; then the problem you were talking about wouldn't
occur, correct?

Thank you again!!

"AMoose" wrote:

> > 2a) How do I determine if an object is a managed or an unmanged resource?
> I
> > understand the basic definition for managed resources are resources that
> the
> > CLR manage and unmanged resources are resources that the CLR doesn't
> manage,
> > however, I haven't been able to find a concrete answer as to what
> resources
> > are manage or not manage by the CLR.
>
> I haven't found a way other than see if the object has implemented the
> IDispose interface and sometimes just for kicks I look at IL just to see
> what may be going on.
>
> I *would* call the dispose method if implemented because leaving unmanaged
> resources open can effect your program in different ways. Either that or
> understand what the effect is of not calling dispose will have on your
> program.
>
> One effect is that unmanaged resources do not pressure the GC for collection
> like managed objects do. The result is that you could end up with a lot of
> unmanaged resources open, putting pressure on the OS. Of course they will be
> cleaned up eventually but only when the GC deems it necessary and its basing
> its decision on managed memory!
>
> Example, take the following line of code and put it in a plain managed
> windows application and make sure it runs on start up (in my example I made
> sure F goes immediately out of scope and made sure there were no other
> references to F):
>
> - Dim F As IO.FileStream = IO.File.Open("somfile", IO.FileMode.Open,
> IO.FileAccess.Write, IO.FileShare.None)
>
> Now try to run a second instance of the application. On my machine I get an
> exception. Go ahead and set F = Nothing and it continues to happen. I got
> up, went to the bathroom, came back and tried to open a second instance
> again and still got an exception (~2 minutes). FileStream has a handle to
> the underlying unmanaged file and the code above is depending on the GC to
> clean up the object. This situation would be catastrophic on a server
> application processing many requests!
>
>
>
>
>
>
>



Relevant Pages

  • RE: what are unmanaged resources
    ... the GC will come along and clean up all the memory and resources associated ... in a deterministic fashion and clean up unmanaged resources. ... placed inside the Dispose functions. ... sooner than later (i.e. by waiting for the garbage collector) and you should ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Disposing managed resources
    ... can, if it needs, to get more resources. ... using the Dispose() method of that interface. ... understand the .NET memory model very well, nor does he really seem to ... Almost all System.Drawing hold unmanaged resources ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: In need of .NET advocacy
    ... awful for unmanaged resources - so don't use it for unmanaged ... The "using" statement makes disposing of unmanaged resources in C# very ... You've cited cases where Java developers have assumed that the GC can ... is that there are plenty of C++ programs which leak memory and wouldn't ...
    (microsoft.public.dotnet.general)
  • Re: GC and dispose
    ... We often use the Dispose for cleaning managed resources ... UNMANAGED resources inside your classes get cleaned up. ...
    (microsoft.public.dotnet.framework)
  • Re: GC and Dispose method questions
    ... > 2a) How do I determine if an object is a managed or an unmanged resource? ... > understand the basic definition for managed resources are resources that ... > CLR manage and unmanged resources are resources that the CLR doesn't ... One effect is that unmanaged resources do not pressure the GC for collection ...
    (microsoft.public.dotnet.languages.vb)