开发者

Memory scope of an anonymous object - C#.Net

I've the following code

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

Will the copy of the (huge) dataset dsTest be persi开发者_如何学Gosted in the memory or will it be Garbage Collected by default?

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


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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜