gpt4 book ai didi

.net - 在 .NET DataTable 中存储数据的内存开销是多少?

转载 作者:行者123 更新时间:2023-12-03 11:20:39 24 4
gpt4 key购买 nike

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

那么任何人都可以对这三种开销中的每一种/任何一种进行估计(并且,我猜,解释!)?

最佳答案

好吧,别忘了 DataTable商店 2? 3?数据的版本 - 原始和更新(可能是另一个?)。它也有很多引用,因为它是基于单元格的,并且可以对任何值类型进行装箱。很难量化确切的内存......

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

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

    // 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 - 在 .NET DataTable 中存储数据的内存开销是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/424598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com