2

i'm new in GC, if i want to release a datatable array, does calling dispose() on all its datatables is enough? do i need to set null to the databale array too ? or to every datatable?

is this enough?:

 DataTable[] myTables = null;
             try
             {
                 myTables = DB.GetTables();

                 // use myTables....
             }
             finally
             {
                 // Dispose Data Tables
                 if (myTables != null)
                 {
                     foreach (DataTable dt in myTables)
                     {
                         if (dt != null)
                             dt.Dispose();
                     }
                 }
             }
1
  • Garbage Collector is Called itself on disposing of Application Nov 7, 2010 at 12:08

3 Answers 3

3

For Garbage Collection you have to do nothing.

Since DataTable implements IDisposable it is a good idea to call Dispose() on all of them (like you are doing). Setting references to null is only necessary in very rare situations. Not here.

But Dispose() is about releasing managed and unmanaged resources, not about Garbage collection.
For DataTbales Dispose() isn't actually necessary, but you don't want to bother knowing that. If a class has a Dispose() member, call it.

9
  • This reads to me like "if you do anything, like dispose stuff or null it out, GC will break". As in, like it's a requirement that you not touch anything. Just FYI.
    – cHao
    Nov 7, 2010 at 12:08
  • @Chao, I was adding some more text. Nov 7, 2010 at 12:09
  • Disposing objects that implement IDisposable is a good general practice, even with a garbage collector. Nov 7, 2010 at 12:10
  • 1
    @Chao: and for GC that is true. Nov 7, 2010 at 12:13
  • 2
    @rodniko: The GC will recycle them later. Nov 7, 2010 at 12:54
2

No, you don't need to do anything more.

The garbage collector knows when the array isn't used any more, and from that point in the code it can collect the array and everything that's inside it. Setting the array reference or any data table references to null is only unneccesary work.

1

Garbage collection is a very costly process, which is why in their wisdom Microsoft decided to automate it such that it runs when the system has spare capacity to run it, or when resources are becoming too low.

As a general rule of thumb therefore, you should only need to worry about explicit garbage collection when you are working with unmanaged objects (such as COM references) or where you are experiencing performance issues and you find that manual garbage collection can assist.

2
  • 1
    In the .net world, the only "manual garbage collection" is calling GC.Collect() directly, and there's hardly ever reason to do that. You may be referring to disposables instead, in which case you may want to mention that.
    – cHao
    Nov 7, 2010 at 12:18
  • thanks cHao. Totally agree with you. Maybe I am wrong, but when I first replied to the question I didn't see the word disposable there, and my terse reply focused solely on GC.
    – Topdown
    Nov 7, 2010 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.