NewRow () table causes memory leak

While researching a memory leak, I discovered that this was caused by repeatedly calling NewRow () on a table inside a loop. However, the created DataRow was never added to the table row collection, and the number of rows in the table never exceeded zero.

My question is why does it use more memory every time NewRow is called, although the newly created DataRow is never 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 line).

Please ignore the problem why the code creates DataRows that are not added to the table!

+5
source share
3 answers

A DataRow inherits a schema from a DataTable, so there are links from a DataRow to the schema of the table that generated the row. The new row is in the "Separate" state in the table.
therefore, the GC left the new unused lines alone.

+5
source

DataTable.NewRow () adds the created row to the DataTable RecordManager. I'm not quite sure why this is happening, but that is why it is not released by the GC.

It seems that there are only two ways to get rid of DataRow:

  • Add it to the table, then delete it.
  • Call DataTable.Clear ().
+4
source

, .

Table.NewRow , , .

This new row should be added to the table using table.Rows.Add (newRow). Your loop will create objects that are never used and therefore will consume memory. See this article for more information http://msdn.microsoft.com/en-us/library/system.data.datatable.newrow.aspx

+1
source

All Articles