简体   繁体   English

匿名对象的内存范围-C#.Net

[英]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 persisted in the memory or will it be Garbage Collected by default? (巨大)数据集dsTest的副本是否将保留在内存中,或者默认情况下将被垃圾回收?

Does it copy the whole dataset to the memory? 是否会将整个数据集复制到内存中? When the GC happens? GC何时发生?

This may actually be two questions: 1) anonymous object lifetime, and 2) dataset lifetime. 这实际上可能是两个问题:1)匿名对象生存期,以及2)数据集生存期。

for 1) - as soon as there are no references to the object, it is eligible to be garbage collected, just like a "named" object. 1)-只要没有对对象的引用,就可以像“命名”对象一样对其进行垃圾回收。

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). 对于2)数据表的defaultview具有对该表的引用,因此该数据表将保留在内存中,直到您不再持有对该视图的引用(或任何行等-引用该数据集的任何内容)为止。

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 . 由于DefaultView引用的对象本身持有对从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. 但是,至少,您的dvTest都会使它持续一段时间。

您正在复制DataTable,然后通过DefaultView在dvTest中保存对它的引用,因此,除非dvTest超出范围,否则它不会被垃圾收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM