views:

49

answers:

3

Hello

I've the following code

DataView dvTest= dsTest.Tables[1].Copy().DefaultView;

Will the copy of the (huge) dataset dsTest be persisted in the memory or will it be Garbage Collected by default?

Does it copy the whole dataset to the memory? When the GC happens?

Regards

NLV

A: 

You are copying the DataTable and then holding a reference to it in dvTest via DefaultView, so no it will not be garbage collected until dvTest goes out of scope.

chibacity
+2  A: 

It is very likely that it will hang around since the object referenced by DefaultView itself holds a reference to the object returned from Copy. And, of course, it will eventually be collected once it becomes unreachable. But, at the very least, your dvTest will cause it to persist for awhile anyway.

Brian Gideon
+3  A: 

This may actually be two questions: 1) anonymous object lifetime, and 2) dataset lifetime.

for 1) - as soon as there are no references to the object, it is eligible to be garbage collected, just like a "named" object.

for 2) The defaultview of a datatable has a reference to the table, so the datatable will remain in memory until you no longer hold a reference to the view (or any rows, etc - anything that references the dataset).

Philip Rieck