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!


I think your two issues are related.

Table.NewRow creates a new data row that has the same column format as the initial Table.

This new row needs to be added to the table using table.Rows.Add(newRow). Your loop will be creating objects that are never used and hence will eat up memory. See this article for more information

Hi Stephen, I understand that, I'm just not sure why it uses memory when the row is never used. I guess I was after some deeper info on what goes on behind the scenes in the DataTable.
Si Keep
+1  A: 

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.

Ovidiu Pacurar
+1  A: 

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().
Rasmus Faber