Google Answers Logo
View Question
 
Q: C# IDisposable ( No Answer,   4 Comments )
Question  
Subject: C# IDisposable
Category: Computers > Programming
Asked by: agilemichael-ga
List Price: $20.00
Posted: 20 Sep 2004 07:01 PDT
Expires: 28 Sep 2004 11:43 PDT
Question ID: 403666
I am using C#.  What if I implement idisposable but never call dispose
on the object?  What effect does this have on the memory usage of my application?
My application will be running continuosly for months at a time. 
Hopefully, it never comes down except for updates.

Clarification of Question by agilemichael-ga on 23 Sep 2004 19:23 PDT
What does IDispose, do then, anyways?

If it has no effect if you do not call it, it doesn't affect garbage collection?

Will it leave the object non garbage collected for a pass or two?

I am concerned if we implement the pattern, then create a very large
object that implements the pattern, we will experience abnormal spikes
in memory usage.

Obrigado
Answer  
There is no answer at this time.

Comments  
Subject: Re: C# IDisposable
From: blowdart-ga on 21 Sep 2004 00:21 PDT
 
It depends on why you implemented IDisposable in the first place.

Generall IDisposable objects implement that interface because they
hold open external resources which you may want to clear before the
garbage collector sweeps up your object, for example the SQL ADO.Net
objects implement IDisposable so you can drop the server connection
and any cursors or other "gubbins" that your operation created
immediatly.

If you've just implemented IDisposable because you wanted a
deterministic destructor consider if you really need one. If you're
not holding locks on files, or external resources does it really
matter when your object is disposed? Remember that you can write a
destructor anyway, as long as you're not accessing any external
resources in it.

The answer really depends on why you've implemented it, and what your
dispose does, as well as how many objects of that type are creating
during the run of your problem.
Subject: Re: C# IDisposable
From: agilemichael-ga on 21 Sep 2004 10:21 PDT
 
We implemented IDisposable on objects that do not meet any of the
reasons for having a deterministic destructor.

Basically, we implemented it wrong.

I want to know what the effect is of having objects implement
IDisposable when they don't need it, and Dispose is never called.

How do these orphaned non-disposed IDisposable objects affect the
garbage collection and performance of the application?

class MyObject : IDisposable
{
private string myExampleString = string.Empty;

public void Dispose()
{
// has noting to do!  no reason for existence
}

}

main()
{
MyObject myObject = new MyObject();

// falls out of scope without having Dispose called explicitly
}
Subject: Re: C# IDisposable
From: blowdart-ga on 21 Sep 2004 13:12 PDT
 
It shouldn't have any effect, unless you're relying on something on
the Dispose method to clear any resources.

If your Dispose is blank, then not calling it has no effect on
anything, including garbage collection.

For further reading Chris Lyon published a blog entry clarifying
destructors early this morning (good timing!). You can find it at
http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx
Subject: Re: C# IDisposable
From: blowdart-ga on 28 Sep 2004 06:18 PDT
 
>What does IDispose, do then, anyways?

Causes confusion :) A dispose method/IDisposable is an optional,
standard way to provide "a method that releases allocated unmanaged
resources".

So, what does that mean? Well, if you're using unmanaged resources,
such as a Sql Connection, a connection to a USB device which isn't
managed, or something like that, and you keep those resources
allocated throughout the lifetime of your object then you should
implement IDisposable. Developers who use your class can then call
Dispose if they wish to free these resources early, before your object
falls out of scope and is cleared by the GC. *However* nothing
enforces that they actually do call it, so you should also call it
yourself in your desctructor (see the previous link I posted for
details on a pattern to do this properly and in a thread safe way)

>If it has no effect if you do not call it, it doesn't affect garbage collection?

No it doesn't. Dispose is not part of garbage collection.

>Will it leave the object non garbage collected for a pass or two?

As it's not part of garbage collection, and the GC doesn't care about
it, no, it doesn't.

>I am concerned if we implement the pattern, then create a very large
>object that implements the pattern, we will experience abnormal spikes
>in memory usage.

That depends on what you mean by "the pattern". Simply having a
Dispose method is not going to affect memory usage greatly, however it
does give a false impression to developers who may come across your
class as they may assume you are holding unmanaged resources.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy