473,662 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SuppressFina lize(this);
}

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

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*********@no spam.nospamwrot e in message
news:eZ******** ******@TK2MSFTN GP03.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.SuppressFina lize(this);
}

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

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.comwro te in message
news:%2******** ********@TK2MSF TNGP03.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*********@no spam.nospamwrot e in message
news:eZ******** ******@TK2MSFTN GP03.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.SuppressFina lize(this);
}

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


Jan 18 '07 #4

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

Similar topics

24
7663
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 Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
5
1988
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 descriptive case is unit test data. We have unit test data classes that generate fake data in the database which we can then test against, and then the unit test data classes delete those records. We can have all of these test data classes...
10
1581
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 Tag.Dispose() ' Free your own state (unmanaged objects). ' Set large fields to null. End Sub
4
2028
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 design-blunder, if not a 100-year sleep. It prevents
4
3323
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, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
12
2441
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 will distill it here. The core of his argument started with his statement: "There is no gain in performance, maintainability or otherwise by implementing the Dispose method if unmanaged resources are not involved." I focused particularly on...
11
2311
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 not as a general rule, or only under certain circumstances... Since it's an easy enough thing to do (http://msdn2.microsoft.com/en-us/library/system.idisposable.aspx), is there any good reason not to make every custom class IDisposable, the same...
47
2139
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 with them: a. Nuke 'em on a GC.Collect call (or equivalent) b. Call Dispose on the object. Yes, I understand that the compiler and the GC doesn't know what IDisposable
1
2894
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 object. I want to encapsulate the Word.Application in a Class that implements IDisposable to avoid this potential memory leak. Here is the code I have:
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7367
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.