Guys,
I've always believed that DataTable would consume more memory than a generic List. I am testing loading a DataTable and loading a List from a SQL Server query. In this case, the DataTable is consuming less memory. I'm getting the top 2000 rows and there are 134 fields per row. One binary field and the rest are standard varchar, int, bit and so on.
How in the world could a DataTable with all it's overhead consume less memory than List? The GC is reporting approx 4mb with DataTable and 5mb with list.
I did test with a few of the NorthWind tables and the list was marginally smaller in those cases.
private void GetMemory()
{
label1.Text = string.Format("{0:0.00} MB", GC.GetTotalMemory(true) / 1024.0 / 1024.0);
}
private void DataTableButton_Click(object sender, EventArgs e)
{
var conn = new SqlConnection(@"Initial Catalog=ADatabase;Data Source=AServer;Integrated Security=True");
conn.Open();
var cmd = new SqlCommand("SELECT TOP 2000 * FROM AManyColumnedTable", conn);
var r = cmd.ExecuteReader();
_rows = new List<object[]>();
//uses more memory
object[] a = null;
while (r.Read())
{
a = new object[r.FieldCount];
r.GetValues(a);
_rows.Add(a);
}
//uses less memory
//_table = new DataTable("TheTable");
//_table.Load(r);
r.Close();
conn.Close();
GetMemory();
}