C#列表<double>大小与双 [] 大小 [英] C# List&lt;double&gt; size vs double[] size

查看:50
本文介绍了C#列表<double>大小与双 [] 大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是在测试 CLR Profiler,我做了一个小程序,它创建了一个包含 1,000,000 个双打的列表.我检查了堆,结果发现 List<> 大小大约为 124KB(我记不清了,但大约是这样).这真的震撼了我的世界,如果里面有 100 万个双打,怎么可能是 124KB?无论如何,在那之后我决定检查双[1000000].令我惊讶的是(实际上并非如此,因为这是我对 List<> =P 的预期),数组大小为 7.6MB.巨大的差异!!

So I just was testing the CLR Profiler from microsoft, and I did a little program that created a List with 1,000,000 doubles in it. I checked the heap, and turns out the List<> size was around 124KB (I don't remember exactly, but it was around that). This really rocked my world, how could it be 124KB if it had 1 million doubles in it? Anyway, after that I decided to check a double[1000000]. And to my surprise (well not really since this is what I expected the with the List<> =P), the array size is 7.6MB. HUGE difference!!

他们怎么不一样?List<> 如何管理它的项目以使其(令人难以置信的)内存高效?我的意思是,这不像其他 7.5 mb 在其他地方,因为在我创建 100 万个双打之后,应用程序的大小大约大了 3 或 4 KB.

How come they're different? How does the List<> manage its items that it's so (incredibly) memory efficient? I mean, it's not like the other 7.5 mb were somewhere else, because the size of the application was around 3 or 4 KB bigger after I created the 1 million doubles.

推荐答案

List 使用数组来存储值/引用,所以我怀疑除了List 增加了多少开销.

List<T> uses an array to store values/references, so I doubt there there will be any difference in size apart from what little overhead List<T> adds.

给定下面的代码

var size = 1000000;
var numbers = new List<double>(size);
for (int i = 0; i < size; i++) {
   numbers.Add(0d);
}

相关对象的堆看起来像这样

the heap looks like this for the relevant object

0:000> !dumpheap -type Generic.List  
 Address       MT     Size
01eb29a4 662ed948       24     
total 1 objects
Statistics:
      MT    Count    TotalSize Class Name
662ed948        1           24 System.Collections.Generic.List`1[[System.Double,  mscorlib]]
Total 1 objects

0:000> !objsize 01eb29a4    <=== Get the size of List<Double>
sizeof(01eb29a4) =      8000036 (    0x7a1224) bytes     (System.Collections.Generic.List`1[[System.Double, mscorlib]])

0:000> !do 01eb29a4 
Name: System.Collections.Generic.List`1[[System.Double, mscorlib]]
MethodTable: 662ed948
EEClass: 65ad84f8
Size: 24(0x18) bytes
 (C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
65cd1d28  40009d8        4      System.Double[]  0 instance 02eb3250 _items    <=== The array holding the data
65ccaaf0  40009d9        c         System.Int32  1 instance  1000000 _size
65ccaaf0  40009da       10         System.Int32  1 instance  1000000 _version
65cc84c0  40009db        8        System.Object  0 instance 00000000 _syncRoot
65cd1d28  40009dc        0      System.Double[]  0   shared   static _emptyArray
    >> Domain:Value dynamic statics NYI
 00505438:NotInit  <<

0:000> !objsize 02eb3250 <=== Get the size of the array holding the data
sizeof(02eb3250) =      8000012 (    0x7a120c) bytes (System.Double[])

所以 List 是 8,000,036 字节,底层数组是 8,000,012 字节.这非常适合引用类型 (Array) 的通常 12 字节开销和双打的 1,000,000 乘以 8 字节.除此之外,List 为上面显示的字段添加了另外 24 字节的开销.

So the List<double> is 8,000,036 bytes, and the underlying array is 8,000,012 bytes. This fits well with the usual 12 bytes overhead for a reference type (Array) and 1,000,000 times 8 bytes for the doubles. On top of that List<T> adds another 24 bytes of overhead for the fields shown above.

结论:我没有看到任何证据表明 List 会比 double[] 占用更少的空间元素的数量.

Conclusion: I don't see any evidence that List<double> will take up less space than double[] for the same number of elements.

这篇关于C#列表<double>大小与双 [] 大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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