在 .NET DataTable 中存储数据的内存开销是多少? [英] What is the memory overhead of storing data in a .NET DataTable?

查看:20
本文介绍了在 .NET DataTable 中存储数据的内存开销是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理与 .NET DataTable 以及表中的各个 DataRow 相关联的内存开销.
换句话说,数据表占用的内存比简单地存储每列数据的正确类型数组所需的内存多多少?
我想会有一些基本的表开销,每列加上一些量,然后每行又增加一些量.

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?

推荐答案

好吧,不要忘记 DataTable 存储 2?3?数据的版本 - 原始的和更新的(可能是另一个?).它也有很多引用,因为它是基于单元格的,以及任何值类型的装箱.很难量化确切的内存......

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...

就我个人而言,我很少使用 DataTable - 在我看来,类型化 POCO 类是一个更明智的选择.我不会(直接)使用数组 - ListBindingList 或类似的会更常见.

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.

作为粗略的衡量标准,您可以创建很多表等并查看内存使用情况;例如,以下显示了一个 ~4.3 的因素 - 即成本是其 4 倍以上,但显然这在很大程度上取决于列数、行数、表等:

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();

对比

    // 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();

(基于)

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; }
}

这篇关于在 .NET DataTable 中存储数据的内存开销是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆