Solved

Performance: Disposing/Releasing objects

Posted on 2004-10-21
177 Views
Last Modified: 2010-04-15
I am a little confused by the Microsoft help on performance.

1. Do I need to dispose of a datatable?

myDataTable.Dispose();              Is it faster or slower to do this?

2. Do I need to create a dispose method for custom objects?

public class MyClass : IDisposable {
  public void Dispose() {
    Dispose(true);
    GC.SuppressFinalizer(this);
  }
  protected virtual void Dispose(bool disposing) {
    if (disposing) {
      ...
    }
      ...
  }

3. If I do create a dispose method for custom objects, then I should not implement Finalize(). Right?
0
Question by:mppeters
    3 Comments
     
    LVL 8

    Accepted Solution

    by:
    1. Alway call the Dispose() method when you don't need an object anymore, or else resources may never be freed.
    2. You don't have to implement IDisposable if your custom objects do not use unmanaged resources. In general, if your custom object does not have members that implement IDisposable, your custom object does not need to do so.
    3. Correct, since all resources are already freed when calling the dispose method.

    HTH,

    Razzie
    0
     
    LVL 8

    Author Comment

    by:mppeters
    Thanks Razzie.

    >>1. Alway call the Dispose() method when you don't need an object anymore, or else resources may never be freed.

    In general if the object has a Dispose() method, I should call it.
    Should I also be setting the disposed object to null?

    >>2. You don't have to implement IDisposable if your custom objects do not use unmanaged resources. In general, if your custom object does not have members that implement IDisposable, your custom object does not need to do so.

    Custom objects that do not use unmanaged resources do not need to implement IDisposable and therefore do not need to have a Dispose() method.

    Should I be setting these custom object instances to null?
    0
     
    LVL 8

    Assisted Solution

    by:Razzie_
    1. Nope, that doesn't matter.
    2. Most of the time, that is not necessary. In some cases, it can even have a negative impact on performance. If you declare an object in a method, it is usually best to simply let it go out off scope without setting it to null.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now