Answered by:
Objects: Dispose or Reuse?

Question
-
Hi, I've been wondering if it's better to dispose an object or reuse it.
Please see my sample because I can't explain it well... :)
private MyObject myobject = new myobject(); private void DoThis(string message) { myobject.DisplayThis(message); }
VERSUS
private void DoThis(string message) { using (MyObject myobject = new myobject()) { myobject.DisplayThis(message); } }
The method will be called over and over again, and please assume that there is a lot of methods inside the object which will also be called over and over again.
Advace thanks guys!
Always remember to say "Thanks", clicking "Mark As Answer" and "Vote as Helpful" if a post answers your question.
Friday, January 25, 2013 8:21 AM
Answers
-
The question is why is that object disposable in the first place? In many cases disposable objects cannot be reused, for example you cannot reuse a FileStream to read from a different file, you have to create a new FileStream.
Disposable issues aside in most cases you shouldn't reuse objects unless:
- the object is very large ( > 85000 bytes in size ), reusing such objects may help avoid problems with the large object heap
- the object is used for long periods of time anyway
The problem with reusing small objects is that the longer you keep using an object the higher the chance it gets moved by GC to the 2nd generation. This increases the size of the 2nd generation unnecessarily and this may have an impact on performance. In addition modifying fields of reference type might be a bit slower for objects in the 2nd generation.
But that's the general case, ultimately it depends a lot on various factors. How often such an object needs to be created, how expensive is to create such an object etc.
- Marked as answer by Arvin Granados Monday, January 28, 2013 12:07 AM
Friday, January 25, 2013 8:34 AM -
Hi Mike, thanks for the answer. But what if that object is a form? Do I need to dispose it every time?
Just call Close() is enough, it will release things automatically.
Look at this reflected code:
public void Close() { if (base.GetState(0x40000)) { throw new InvalidOperationException(SR.GetString("ClosingWhileCreatingHandle", new object[] { "Close" })); } if (base.IsHandleCreated) { this.closeReason = CloseReason.UserClosing; base.SendMessage(0x10, 0, 0); } else { base.Dispose(); } }
- Proposed as answer by Christopher84 Friday, January 25, 2013 5:21 PM
- Marked as answer by Jason Dot Wang Monday, February 4, 2013 8:04 AM
Friday, January 25, 2013 8:44 AM -
If you want to reuse it, call Hide instead of close so that you can show it again. Keep in mind Mike Danes comments, though.
- Proposed as answer by Christopher84 Friday, January 25, 2013 5:20 PM
- Marked as answer by Jason Dot Wang Monday, February 4, 2013 8:04 AM
Friday, January 25, 2013 5:16 PM
All replies
-
Just depends.
If your object is light-weighted (I mean it doesn't take much memory). U can keep it and when you use the same variable to create a new instace, it will be automatically garbaged.
But if you think ur object will take much money or it has something like unmanaged codes. U have to write a final method with a Dispose one (Dispose method has nothing to do with final function, but it's a way to let you release things inside immediately instead of letting garbage collection automatically, because it's unknown when the Garbage starts).
Friday, January 25, 2013 8:29 AM -
The question is why is that object disposable in the first place? In many cases disposable objects cannot be reused, for example you cannot reuse a FileStream to read from a different file, you have to create a new FileStream.
Disposable issues aside in most cases you shouldn't reuse objects unless:
- the object is very large ( > 85000 bytes in size ), reusing such objects may help avoid problems with the large object heap
- the object is used for long periods of time anyway
The problem with reusing small objects is that the longer you keep using an object the higher the chance it gets moved by GC to the 2nd generation. This increases the size of the 2nd generation unnecessarily and this may have an impact on performance. In addition modifying fields of reference type might be a bit slower for objects in the 2nd generation.
But that's the general case, ultimately it depends a lot on various factors. How often such an object needs to be created, how expensive is to create such an object etc.
- Marked as answer by Arvin Granados Monday, January 28, 2013 12:07 AM
Friday, January 25, 2013 8:34 AM -
If can't really figure out what you have there from these extremly short code fragments.
But one question leaps to mind:
Do you actually need an object of the class? If all your class does is containing this function jsut make the function static and you don't need a single isntance of the object.All using does is reliably call the MyObject.Dispose() method (even on error) and the only reason to even have a dispose method is that the object has some unmanaged resources (database connections, filesystem-access) that just have to be cleared at a fixed time (in langaugages without Garbage Collection engine, Destructors fullfill the role of Dispose).
Source:'
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx"The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.
Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.
It is a version-breaking change to add the IDisposable interface to an existing class, because it changes the semantics of the class."
- Edited by Christopher84 Friday, January 25, 2013 8:39 AM
Friday, January 25, 2013 8:38 AM -
Hi Mike, thanks for the answer. But what if that object is a form? Do I need to dispose it every time?
Always remember to say "Thanks", clicking "Mark As Answer" and "Vote as Helpful" if a post answers your question.
Friday, January 25, 2013 8:41 AM -
Hi Mike, thanks for the answer. But what if that object is a form? Do I need to dispose it every time?
Just call Close() is enough, it will release things automatically.
Look at this reflected code:
public void Close() { if (base.GetState(0x40000)) { throw new InvalidOperationException(SR.GetString("ClosingWhileCreatingHandle", new object[] { "Close" })); } if (base.IsHandleCreated) { this.closeReason = CloseReason.UserClosing; base.SendMessage(0x10, 0, 0); } else { base.Dispose(); } }
- Proposed as answer by Christopher84 Friday, January 25, 2013 5:21 PM
- Marked as answer by Jason Dot Wang Monday, February 4, 2013 8:04 AM
Friday, January 25, 2013 8:44 AM -
Depends on how the form is displayed.
If you use ShowDialog then you can reuse it but IMO you shouldn't do that unless you want to preserve some UI state (a listbox selection or scroll position for example) or if form creation is really expensive for whatever reason. Some simple dialog (like Notepad's Find dialog for example) shouldn't be reused.
If you use Show to display the form then normally you cannot reuse it, when the user closes such a form WinForms automatically disposes it and it's not possible to show it again.
Friday, January 25, 2013 8:48 AM -
If you want to reuse it, call Hide instead of close so that you can show it again. Keep in mind Mike Danes comments, though.
- Proposed as answer by Christopher84 Friday, January 25, 2013 5:20 PM
- Marked as answer by Jason Dot Wang Monday, February 4, 2013 8:04 AM
Friday, January 25, 2013 5:16 PM