Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table NewRow() Causes Memory Leak

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!

like image 924
Simon Keep Avatar asked Oct 28 '08 09:10

Simon Keep


2 Answers

DataTable.NewRow() adds the created row to the DataTable's RecordManager. I am not entirely sure why this happens, but this is why it is not freed by the GC.

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

  1. Add it to the table, then delete it.
  2. Call DataTable.Clear().
like image 106
Rasmus Faber Avatar answered Oct 16 '22 18:10

Rasmus Faber


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.

like image 23
Ovidiu Pacurar Avatar answered Oct 16 '22 16:10

Ovidiu Pacurar