Re: GC and Dispose method questions
From: Teresa (Teresa_at_discussions.microsoft.com)
Date: 01/14/05
- Next message: jonathan_at_solomon1.org: "Re: How to read a text file that is being used by another process?"
- Previous message: Peter Huang: "RE: Using threads to run multiple processes at Startup"
- In reply to: AMoose: "Re: GC and Dispose method questions"
- Next in thread: Cor Ligthert: "Re: GC and Dispose method questions"
- Reply: Cor Ligthert: "Re: GC and Dispose method questions"
- Reply: AMoose: "Re: GC and Dispose method questions"
- Messages sorted by: [ date ] [ thread ]
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!
>
>
>
>
>
>
>
- Next message: jonathan_at_solomon1.org: "Re: How to read a text file that is being used by another process?"
- Previous message: Peter Huang: "RE: Using threads to run multiple processes at Startup"
- In reply to: AMoose: "Re: GC and Dispose method questions"
- Next in thread: Cor Ligthert: "Re: GC and Dispose method questions"
- Reply: Cor Ligthert: "Re: GC and Dispose method questions"
- Reply: AMoose: "Re: GC and Dispose method questions"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|