473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Disposing managed resources

Hello,

was wondering of how to dispose of managed resources?

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

http://www.marcclifton.com/tabid/79/Default.aspx
http://www.codeproject.com/managedcp...collection.asp - sources

Thanks!

Oct 2 '07 #1
8 8954
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/managedcp...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
Oct 3 '07 #2
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/...24/140645.aspx and links.

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comha scritto nel messaggio
news:13*************@corp.supernews.com...
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/managedcp...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
Oct 3 '07 #3
On Oct 3, 9:49 am, "Laura T." <LT_s...@yahoo.comwrote:
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.
Seehttp://blogs.msdn.com/brada/archive/2004/05/24/140645.aspxand links.

"Peter Duniho" <NpOeStPe...@NnOwSlPiAnMk.comha scritto nel messaggionews:13*************@corp.supernews.com.. .
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/managedcp...ollection.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
there are so many sources about how to use the IDisposable.... that's
what i thought that an image is an unmanaged resource.

so how the GC.SuppressFinalize comes into use? I read that this may
improve slightly insignificantly the performance... so that managed
object doesn't need to be finalized again because of its managed
resource. how would the SuppressFinalize come into use?

Oct 3 '07 #4

"Varangian" <of****@gmail.comha scritto nel messaggio
news:11**********************@o80g2000hse.googlegr oups.com...
On Oct 3, 9:49 am, "Laura T." <LT_s...@yahoo.comwrote:
>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.
Seehttp://blogs.msdn.com/brada/archive/2004/05/24/140645.aspxand links.

"Peter Duniho" <NpOeStPe...@NnOwSlPiAnMk.comha scritto nel
messaggionews:13*************@corp.supernews.com. ..
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/managedcp...ollection.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

there are so many sources about how to use the IDisposable.... that's
what i thought that an image is an unmanaged resource.
The managed System.Drawing.Image holds an unmanaged resource (GDI handle),
so it must be disposed.
It's not 100% managed. Almost all System.Drawing hold unmanaged resources
and thus must be disposed as soon as possible.
Same goes for System.IO.* and many others.
so how the GC.SuppressFinalize comes into use? I read that this may
improve slightly insignificantly the performance... so that managed
object doesn't need to be finalized again because of its managed
resource. how would the SuppressFinalize come into use?
GC.SuppressFinalize() is intended only for Dispose(). It tells CLR that that
"I'm done. No more cleanup needed, thanks", by clearing a virtual "sorry,
could you please clean me up because someone forgot to do it?" flag.
CLR says "OK, thanks for the cooperation. Now I can just deallocate you
which is faster than remembering to call your Finalizer(), call it and then
deallocate. Good."
If you don't call (but you do) Dispose(), the object still has the "sorry,
could you please clean me up because someone forgot to do it?" fto lag set,
so CLR will take the trouble (but it makes it angry) to call the Finalizer()
(someday) to clear the flag and then deallocate.

So from your point of you (consumer of the object), you do not need (I'd say
must not) to call GC.SuppressFinalize() (taken that the Dispose() was
written correctly).
It won't help.

Oct 3 '07 #5
On Oct 3, 12:37 pm, "Laura T." <LT_s...@yahoo.comwrote:
"Varangian" <ofm...@gmail.comha scritto nel messaggionews:11**********************@o80g2000hse .googlegroups.com...
On Oct 3, 9:49 am, "Laura T." <LT_s...@yahoo.comwrote:
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.
Seehttp://blogs.msdn.com/brada/archive/2004/05/24/140645.aspxandlinks.
"Peter Duniho" <NpOeStPe...@NnOwSlPiAnMk.comha scritto nel
messaggionews:13*************@corp.supernews.com.. .
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/managedcp...on.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
there are so many sources about how to use the IDisposable.... that's
what i thought that an image is an unmanaged resource.

The managed System.Drawing.Image holds an unmanaged resource (GDI handle),
so it must be disposed.
It's not 100% managed. Almost all System.Drawing hold unmanaged resources
and thus must be disposed as soon as possible.
Same goes for System.IO.* and many others.
so how the GC.SuppressFinalize comes into use? I read that this may
improve slightly insignificantly the performance... so that managed
object doesn't need to be finalized again because of its managed
resource. how would the SuppressFinalize come into use?

GC.SuppressFinalize() is intended only for Dispose(). It tells CLR that that
"I'm done. No more cleanup needed, thanks", by clearing a virtual "sorry,
could you please clean me up because someone forgot to do it?" flag.
CLR says "OK, thanks for the cooperation. Now I can just deallocate you
which is faster than remembering to call your Finalizer(), call it and then
deallocate. Good."
If you don't call (but you do) Dispose(), the object still has the "sorry,
could you please clean me up because someone forgot to do it?" fto lag set,
so CLR will take the trouble (but it makes it angry) to call the Finalizer()
(someday) to clear the flag and then deallocate.

So from your point of you (consumer of the object), you do not need (I'd say
must not) to call GC.SuppressFinalize() (taken that the Dispose() was
written correctly).
It won't help.
thanks Laura .T.

the last part I didn't get it quite correctly... you said (taken that
the Dispose() was written correctly) how can you say that if the
Dispose method will supposedly do the SuppressFinalize?

Oct 3 '07 #6
Varangian wrote:
[...]
there are so many sources about how to use the IDisposable.... that's
what i thought that an image is an unmanaged resource.
The classes derived from Image do have unmanaged resources. That's why
you should call Dispose() on them when you're done with them.

But you asked about disposing _managed_ resources. As both Laura and I
have mentioned, managed resources are already managed. You should not
usually need to do anything explicit with them in the way of disposal.
so how the GC.SuppressFinalize comes into use? I read that this may
improve slightly insignificantly the performance... so that managed
object doesn't need to be finalized again because of its managed
resource. how would the SuppressFinalize come into use?
As Laura mentioned, SuppressFinalize() is called by the managed object
when it is disposed, so that the finalizing code knows that it doesn't
need to run the class's finalizer when cleaning up the managed object.

Without calling SuppressFinalize(), a managed object will be finalized
as part of the garbage collection behavior. This is so that classes
that have unmanaged resources to be disposed get an opportunity to do so
even if the code that created the instance of that class did not call
Dispose().

Since the point is to catch the cases where some code erroneously fails
to call Dispose(), it is helpful to have a way to signal to the garbage
collector that Dispose() _has_ been called (or for whatever reason,
finalizing isn't required, though since finalizing is related to
IDisposable being implemented it seems to me that the questions of
whether Dispose() has been called and whether the finalizer should be
called go hand-in-hand). The SuppressFinalize() method is the way this
is done.

So, calling SuppressFinalize() is something that the class implementing
IDisposable does, in the Dispose() method. That way it doesn't have to
be unnecessarily disposed again by the finalizing logic.

Pete
Oct 3 '07 #7
So I would be correct if it will (may) improve slightly the
performance when using GC.SuppressFinalize() on an instance?
even though inegligible? based on what you said!

Thanks Pete!
On Oct 3, 7:00 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
Varangian wrote:
[...]
there are so many sources about how to use the IDisposable.... that's
what i thought that an image is an unmanaged resource.

The classes derived from Image do have unmanaged resources. That's why
you should call Dispose() on them when you're done with them.

But you asked about disposing _managed_ resources. As both Laura and I
have mentioned, managed resources are already managed. You should not
usually need to do anything explicit with them in the way of disposal.
so how the GC.SuppressFinalize comes into use? I read that this may
improve slightly insignificantly the performance... so that managed
object doesn't need to be finalized again because of its managed
resource. how would the SuppressFinalize come into use?

As Laura mentioned, SuppressFinalize() is called by the managed object
when it is disposed, so that the finalizing code knows that it doesn't
need to run the class's finalizer when cleaning up the managed object.

Without calling SuppressFinalize(), a managed object will be finalized
as part of the garbage collection behavior. This is so that classes
that have unmanaged resources to be disposed get an opportunity to do so
even if the code that created the instance of that class did not call
Dispose().

Since the point is to catch the cases where some code erroneously fails
to call Dispose(), it is helpful to have a way to signal to the garbage
collector that Dispose() _has_ been called (or for whatever reason,
finalizing isn't required, though since finalizing is related to
IDisposable being implemented it seems to me that the questions of
whether Dispose() has been called and whether the finalizer should be
called go hand-in-hand). The SuppressFinalize() method is the way this
is done.

So, calling SuppressFinalize() is something that the class implementing
IDisposable does, in the Dispose() method. That way it doesn't have to
be unnecessarily disposed again by the finalizing logic.

Pete

Oct 10 '07 #8
Varangian wrote:
So I would be correct if it will (may) improve slightly the
performance when using GC.SuppressFinalize() on an instance?
even though inegligible? based on what you said!
My understanding is that, assuming you've written the rest of your code
correctly, adding a call to GC.SuppressFinalize() will have no effect on
performance (other than the extra time you waste making the call).

Pete
Oct 10 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
5
by: Mathias L. | last post by:
I have two questions for which I couldnt find answer: If I programaticaly close DialogForm (calling Close()), is it enough or do I have to dispose it as MS.NET help says? Also, in overriden...
7
by: Claire | last post by:
How do I make sure Ive disposed of my objects please? Ive added interface IDispose to a base class. Ive several sub classes inheriting from this base class. I store one or other of these...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
4
by: gmccallum | last post by:
This whole dispose topic confuses me regarding managed and unmanaged resources. I think that it was easier when you just cleaned up every object, which brings me to my problem. I have some...
4
by: sunitabalakrishna | last post by:
Hi, Could someone explain me if I need to implement the Dispose pattern on managed objects that hold objects which have longer life or is a singleton? Example - Class A holds Class Global...
10
by: sternr | last post by:
Hey, I have a C# assembly that inherits from ServicedComponent and is installed in the Com+ with ServerActivation. And I have a C++ application that calls this Com+ object successfully, But the...
4
by: Peter Webb | last post by:
I am supposed to manually dispose of some instances, such as Brushes, right? I have a couple of questions: 1. I have the following code, and it works just fine: ...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.