Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the memory overhead of storing data in a .NET DataTable?

Tags:

I'm trying to get a handle on the amount of memory overhead associated with a .NET DataTable, and with individual DataRows within a table.
In other words, how much more memory does a data table occupy than what would be needed simply to store a properly typed array of each column of data?
I guess there will be some basic table overhead, plus some amount per column, and then again an additional amount per row.

So can anyone give an estimate (and, I guess, explanation!) of each/any of these three kinds of overhead?

like image 847
Nick Avatar asked Jan 08 '09 15:01

Nick


People also ask

What is the maximum size of DataTable in C#?

The maximum number of rows that a DataTable can store is 16,777,216.

What is DataTable data?

Description. DataTables can obtain the data it is to display in the table's body from a number of sources, including being passed in as an array of row data using this initialisation parameter. As with other dynamic data sources, arrays or objects can be used for the data source for each row, with columns.

Why do we use DataTable?

A DataTable object represents tabular data as an in-memory, tabular cache of rows, columns, and constraints. A DataTable object represents tabular data as an in-memory, tabular cache of rows, columns, and constraints. You typically use the DataTable class to perform any disconnected data access.


1 Answers

Well, don't forget that a DataTable stores 2? 3? versions of the data - original and updated (possibly one other?). It also has a lot of references since it is cell-based, and boxing for any value-types. It would be hard to quantify the exact memory...

Personally, I very rarely use DataTable - typed POCO classes are a much more sensible bet in my view. I wouldn't use an array (directly), though - List<T> or BindingList<T> or similar would be far more common.

As a crude measure, you could create a lot of tables etc and look at the memory usage; for example, the following shows a ~4.3 factor - i.e. more than 4 times as expensive, but obviously that depends a lot on the number of columns vs rows vs tables etc:

    // takes **roughly** 112Mb  (taskman)     List<DataTable> tables = new List<DataTable>();     for (int j = 0; j < 5000; j++)     {         DataTable table = new DataTable("foo");         for (int i = 0; i < 10; i++)         {             table.Columns.Add("Col " + i, i % 2 == 0 ? typeof(int)                                 : typeof(string));         }         for (int i = 0; i < 100; i++)         {             table.Rows.Add(i, "a", i, "b", i, "c", i, "d", i, "e");         }         tables.Add(table);     }     Console.WriteLine("done");     Console.ReadLine(); 

vs

    // takes **roughly** 26Mb (taskman)     List<List<Foo>> lists = new List<List<Foo>>(5000);     for (int j = 0; j < 5000; j++)     {         List<Foo> list = new List<Foo>(100);         for (int i = 0; i < 100; i++)         {             Foo foo = new Foo { Prop1 = "a", Prop3 = "b",                  Prop5 = "c", Prop7 = "d", Prop9 = "e"};             foo.Prop0 = foo.Prop2 = foo.Prop4 = foo.Prop6 = foo.Prop8 = i;             list.Add(foo);         }         lists.Add(list);     }     Console.WriteLine("done");     Console.ReadLine(); 

(based on)

class Foo {     public int Prop0 { get; set; }     public string Prop1 { get; set; }     public int Prop2 { get; set; }     public string Prop3 { get; set; }     public int Prop4 { get; set; }     public string Prop5 { get; set; }     public int Prop6 { get; set; }     public string Prop7 { get; set; }     public int Prop8 { get; set; }     public string Prop9 { get; set; } } 
like image 98
Marc Gravell Avatar answered Oct 28 '22 11:10

Marc Gravell