Re: Disposing managed resources



In fact, if the resource is "managed", then heck, let it be managed. :-)

The only case where disposing managed resources *may* be useful is "acquire late"/"release early" scenario.
That is, from the performance point of view, if you know that a big (big) memory structure is not needed anymore but normally it would still be rooted, it may help GC to dispose it and release references early so that GC can, if it needs, to get more resources.
But it's not deterministic.
See http://blogs.msdn.com/brada/archive/2004/05/24/140645.aspx and links.

"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> ha scritto nel messaggio news:13g5o4hhmq4gi46@xxxxxxxxxxxxxxxxxxxxx
Varangian wrote:
was wondering of how to dispose of managed resources?

It depends. Some classes implement IDisposable, and in some of those cases, there are managed resources that can be "released" (unreferenced) using the Dispose() method of that interface.

But I think that normally, there's not any need to dispose of managed resources. It's the unmanaged ones you care about. Managed ones are, by virtue of being managed, going to be cleaned up when there's a good reason to clean them up, and otherwise left alone.

Since you shouldn't waste time cleaning something up when there's not a good reason to, I think that for managed resources, just managing the lifetime of the resource relative to your own use is sufficient.

More typically, you would use IDisposable to dispose of a resource that is either unmanaged, or itself has unmanaged resources and thus implements IDisposable.

or referencing every member of a class to null will release
resources...?

Setting references to null can allow a resource to be released. But it doesn't necessarily do so right away. And I think that for managed resources, you shouldn't even care whether they are or not.

http://www.marcclifton.com/tabid/79/Default.aspx

This seems to discuss the use of IDisposable to release unmanaged resources. It seems to me, however, that the author doesn't really understand the .NET memory model very well, nor does he really seem to understand the true purpose of IDisposable (and he also doesn't seem to understand that when you read a compressed image file to memory, of course it has to be decompressed to be used and so occupies much more memory than it does on disk...but that's a whole other issue :) ).

http://www.codeproject.com/managedcpp/garbage_collection.asp - sources

This article seems much better, but I'm not clear on how it is specifically related to your question. It doesn't seem to me to discuss the question of disposing managed resources per se, though it does discuss IDisposable.

Pete

.



Relevant Pages

  • Re: memory&handles growth due to not disposing command?
    ... > This is the primary function of Dispose. ... > release managed resources when it is not called from within GC. ... Cleaning Up Unmanaged Resources and Implementing a Dispose Method. ...
    (microsoft.public.dotnet.framework.adonet)
  • Implementing IDisposible on light objects
    ... neither unmanaged resources nor managed resources which are resource ... if I write an empty Dispose() method: ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: memory&handles growth due to not disposing command?
    ... >> This is the primary function of Dispose. ... >> release managed resources when it is not called from within GC. ... > This is written in the new documentation about the public one ... Miha Markic ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Disposing managed resources
    ... Some classes implement IDisposable, and in some of those cases, there are managed resources that can be "released" using the Dispose() method of that interface. ... More typically, you would use IDisposable to dispose of a resource that is either unmanaged, or itself has unmanaged resources and thus implements IDisposable. ... It seems to me, however, that the author doesn't really understand the .NET memory model very well, nor does he really seem to understand the true purpose of IDisposable (and he also doesn't seem to ...
    (microsoft.public.dotnet.languages.csharp)

Loading