List <object []> uses more memory than DataTable?

I always thought that a 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 consumes less memory. I get the top 2000 rows and 134 fields per row. One binary field, and the rest are standard varchar, int, bit, etc.

How can there be less DataTable with List overhead in the world than List? GC reports about 4 MB using a DataTable and 5mb with a list.

I tested several NorthWind tables, and in these cases the list was a little smaller.

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();
    }
+5
source share
3 answers

. - GC , , , , . , , .

, , , .

, , , , . , int 20 , , 4 -.

+4

TrimExcess , . , , , .

, . , , , , . , , .

+1

. , , , . 50 , . 200 . DataTable ( 8 ) , 95 ( ). List (Using IDataReader) , (push push) 15 MB ( , ). , DataTable, 1657 , - 850 .

0
source

All Articles