473,406 Members | 2,633 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,406 software developers and data experts.

IDisposable with managed code

If your class implements IDisposable, I was told that this increases the
speed with which your class is garbage collected. Is this true? And if so,
how much "time" does it save? Assume that we are dealing with 100% managed
code and that the clean-up is of big inmemory data structures.

Thanks in advance.

Mark
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.BigData != null)
{
this.BigData.Dispose();
}
}
}
Jan 18 '07 #1
3 2124

Mark wrote:
If your class implements IDisposable, I was told that this increases the
speed with which your class is garbage collected. Is this true?
No, it is not true. IDisposable allows you some control over when
non-memory resources are released: file locks, memory allocated outside
the managed environment, handles to resources in the underlying O/S,
etc. IDisposable has no effect upon when the GC will reclaim memory.

If you have just released a very large memory structure and want to
hasten its collection, you should call GC.Collect(), but even this does
not guarantee immediate cleanup, it just makes it more likely.

Jan 18 '07 #2
Mark,

It is not IDisposable that affects garbage collection it is the type
finalizer (destructor in C#). You can implement IDisposable without having
finalizer, but usually types that implement IDisposable declare finalizers
too just to make sure that the object is disposed even if the programmer
doesn't call Dispose method explicitly.

How finalizers affect GC? I wouldn't say that they slowdown finalization
process; they just need 2 GC cicles in order to be garbage collected. On the
first cicle the finalizer is called and on the second the mempory is marked
as free. Ofcourse they may increase the time needed by the GC because there
is one more finalizer that needs to be ran, but as long as the finalizer
itself doesn't take long time you shouldn't worry about it.

Keep in mind that not all types needs to implement IDisposable. It is
usually implement if the type holds on unmanaged resources that needs to be
cleaned up. If the class holds only managed resource in most of the cases
there is no need of IDisposable and/or finalizers.
--
HTH
Stoitcho Goutsev (100)

"Mark" <ma*********@nospam.nospamwrote in message
news:eZ**************@TK2MSFTNGP03.phx.gbl...
If your class implements IDisposable, I was told that this increases the
speed with which your class is garbage collected. Is this true? And if
so, how much "time" does it save? Assume that we are dealing with 100%
managed code and that the clean-up is of big inmemory data structures.

Thanks in advance.

Mark
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.BigData != null)
{
this.BigData.Dispose();
}
}
}

Jan 18 '07 #3
Hi,

I'd just like to add that IDisposable is usually implemented when
IDisposable types are composited, regardless of whether the class itself has
unmanaged resources to be disposed.

--
Dave Sexton
http://davesexton.com/blog

"Stoitcho Goutsev (100)" <10*@100.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Mark,

It is not IDisposable that affects garbage collection it is the type
finalizer (destructor in C#). You can implement IDisposable without having
finalizer, but usually types that implement IDisposable declare finalizers
too just to make sure that the object is disposed even if the programmer
doesn't call Dispose method explicitly.

How finalizers affect GC? I wouldn't say that they slowdown finalization
process; they just need 2 GC cicles in order to be garbage collected. On
the first cicle the finalizer is called and on the second the mempory is
marked as free. Ofcourse they may increase the time needed by the GC
because there is one more finalizer that needs to be ran, but as long as
the finalizer itself doesn't take long time you shouldn't worry about it.

Keep in mind that not all types needs to implement IDisposable. It is
usually implement if the type holds on unmanaged resources that needs to
be cleaned up. If the class holds only managed resource in most of the
cases there is no need of IDisposable and/or finalizers.
--
HTH
Stoitcho Goutsev (100)

"Mark" <ma*********@nospam.nospamwrote in message
news:eZ**************@TK2MSFTNGP03.phx.gbl...
>If your class implements IDisposable, I was told that this increases the
speed with which your class is garbage collected. Is this true? And if
so, how much "time" does it save? Assume that we are dealing with 100%
managed code and that the clean-up is of big inmemory data structures.

Thanks in advance.

Mark
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.BigData != null)
{
this.BigData.Dispose();
}
}
}


Jan 18 '07 #4

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

Similar topics

24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
5
by: Samuel R. Neff | last post by:
I'd like some opinions on whether or not to use IDisposable for classes that require clean-up but when the clean-up is not related to unmanaged resources or other disposable objects. The most...
10
by: Dennis | last post by:
VB.Net Documentation for implementing IDisposable has: Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean) If disposing Then ' Free other state (managed objects). End If...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
12
by: Cordell Lawrence \(News Group\) | last post by:
There an ongoing discussion between a colleague and myself about the usefulness of the IDisposable pattern beyond the reclamation of unmanaged resources. The discussion is somewhat lengthy so I...
11
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or...
47
by: Hilton | last post by:
Hi, I'm sure I'm simplifying things here, but how about if the GC did this to objects that implement IDisposable: 1. Always Generation 1 (I think that is the correct name) 2. Get aggressive...
1
by: =?Utf-8?B?c3VydHVyeg==?= | last post by:
Hi, I'm using VB2005 + Office XP Enterprise. If you create a Word.Application object, you risk a memory leak if your application crashes because Word.Application is an unmanaged COM Interop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.