跟小D每日学口语

Table NewRow() Causes Memory Leak

Question : Whilst investigating a memory leak I discovered that it was caused by calling NewRow() on a Table inside a loop many times. However the DataRow created was never added to the Table Rows collection and the Table Rows Count never got above zero.

My question is why does this use up more memory every time NewRow is called even though the newly created DataRow never gets added to the Rows collection and the DataRow returned from NewRow is always assigned to the same local variable (thereby apparently discarding the last new row).

Please ignore the issue of why the code is creating DataRows that don't get added to the table!

 

Answer : 

DataRow inherits schema from the DataTable, so there are references from DataRow to the table schema that generated the row. The new row is in Detached state in the table.
this is why GC left the new unused rows alone.

posted @ 2012-03-31 18:57  Danny Chen  阅读(292)  评论(0编辑  收藏  举报