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!
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With