9
votes

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!

4

4 Answers

7
votes

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().
6
votes

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.

1
votes

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 http://msdn.microsoft.com/en-us/library/system.data.datatable.newrow.aspx

0
votes

If you create newRow = table.NewRow() and then in the code below decide than this newRow you more not need. 1) Add if row in the table. 2) Remove, it will prevent memory leak.

    var table = new DataTable();
    while (true)
    {
        var newRow = table.NewRow();
        table.Rows.Add(newRow);   //Without this 2 rows you will have memory leak.
        table.Rows.Remove(newRow);//
    }