Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Performance: Disposing/Releasing objects

Posted on 2004-10-21
3
Medium Priority
?
179 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
Comment
Question by:mppeters
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 1000 total points
ID: 12371649
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
ID: 12372527
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_
Razzie_ earned 1000 total points
ID: 12373008
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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