0

I'm wondering if there is a benefit to clearing data tables of information once you are done, is there a noticeable problem if I don't clean the tables out. I know the process of clearing the table out is only one line, but I"m wondering the benefits it's providing, and if the tables will automatically be cleared when I exit a run of the application or will they remain until a computer is restarted?

Example:

Me.dtSet.Tables("ExampleTable").Clear()
AlsoIvan
  • 55
  • 6
  • For which purpose? Do you have a specific problem in mind? Memory management is done automatically by .NET and you are not supposed to interfere, unless absolutely necessary. When your data table object goes out of scope it should be cleaned up automatically. Please describe your scenario in more detail. – Victor Zakharov May 30 '14 at 18:27
  • @Neolisk I guess what I'm trying to say is that I've been told it's good practice to remove the tables from a dataset when you are done manipulating the data, and I'm wondering not why I'm told that; but what benefits, if there are any to handling the Table removal yourself. – AlsoIvan May 30 '14 at 18:30
  • You should really only care about the scope of your variables, then .NET will do the cleanup for you. Where is your data set declared? – Victor Zakharov May 30 '14 at 18:32
  • @Neolisk it's declared in a class that's used to run the application. – AlsoIvan May 30 '14 at 18:36
  • If it's declared in a class, it will be disposed when this class instance goes out of scope. Is it okay for your purposes? If not, please explain why. – Victor Zakharov May 30 '14 at 18:37
  • @Neolisk Like I mentioned, I'm not sure if I need it for anything or not, I was asking about the benefits of clearing the Tables in general really, but I think between you and Murder I've gotten the answer I was looking for – AlsoIvan May 30 '14 at 18:41
  • In general, if you are unsure whether you need something or not, a rule of thumb is no, you don't need it. :) There is no "general", when it comes to memory management. Actually, there is, called garbage collection, but .NET does it already. Anything else is specific to your issue. Are you having memory leaks? Too much paging? Then it's time to look into it. – Victor Zakharov May 30 '14 at 18:46
  • @Neolisk alright, thank you two so much for clearing this up for me. I do appreciate it – AlsoIvan May 30 '14 at 18:47

1 Answers1

0

Please, see this thread

It essentially states that there is no benefit to disposing of a DataSet / DataTable.

Also:

DataSet and DataTable don't actually have any unmanaged resources, so Dispose() doesn't actually do much. The Dispose() methods in DataSet and DataTable exists ONLY because of side effect of inheritance - in other words, it doesn't actually do anything useful in the finalization.

It turns out that DataSets, DataViews, DataTables suppress finalization in their constructorsc this is why calling Dispose() on them explicitly does nothing.

Presumably, this happens because, as mentioned above, they don’t have unmanaged resources; so despite the fact that MarshalByValueComponent makes allowances for unmanaged resources, these particular implementations don’t have the need and can therefore forgo finalization.

Overview of this Immense Answer:

Without a doubt, Dispose should be called on any Finalizable objects.

DataTables are Finalizable.

Calling Dispose significantly speeds up the reclaiming of memory.

MarshalByValueComponent calls GC.SuppressFinalize(this) in its Dispose() - skipping this means having to wait for dozens if not hundreds of Gen0 collections before memory is reclaimed.

From here, By: Killercam

If those don't fully answer your question, perhaps read this answer.

An important takeaway to your question ->

"Does Dispose() method does not free up the memory & make object as null ??

Dispose and the disposal pattern is not for reclaiming managed memory or "deleting" managed objects (both things you cannot do and what the Garbage Collector is there for), it is for handling the disposal/release of unmanaged resources or other managed resources that have releasable items, such as SqlConnection. It certainly won't null the reference, but may make it unusable from the time of disposal forwards."

Community
  • 1
  • 1
Mark C.
  • 6,332
  • 4
  • 35
  • 71