Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net System.Collections.Generic.List<;T>;内存大小_.net_Memory Management - Fatal编程技术网

.net System.Collections.Generic.List<;T>;内存大小

.net System.Collections.Generic.List<;T>;内存大小,.net,memory-management,.net,Memory Management,只是想知道是否有人试图确定内存中System.Collections.Generic.List的实际大小 我目前正在使用System.HttpRuntime.Cache缓存一个这样的对象,它似乎工作得很好,但我希望能够确定对我的服务器的实际影响 using (System.IO.MemoryStream stream = new System.IO.MemoryStream(capacity)) { new System.Runtime.Serialization.Forma

只是想知道是否有人试图确定内存中System.Collections.Generic.List的实际大小

我目前正在使用System.HttpRuntime.Cache缓存一个这样的对象,它似乎工作得很好,但我希望能够确定对我的服务器的实际影响

using (System.IO.MemoryStream stream = new System.IO.MemoryStream(capacity)) 
{ 
        new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(stream, obj); 
        thisSerialized = stream.ToArray(); 
        return thisSerialized.Length; 
} 

这适用于其他人,但不适用于我需要的内容。

列表的序列化大小将完全无关

一个
列表
将有一点开销(两个
int
s)和一个大小
容量的数组

引用类型的数组每个元素使用
IntPtr.Size
(4或8)字节的内存;值类型数组使用每个元素的
Marshal.SizeOf(type)
字节内存。
(阵列也有一些开销)


列表中的任何引用类型(或列表中的结构中的引用类型)都将单独使用内存。

我从未尝试使用IntPtr获取大小,如果您碰巧有代码段,我会喜欢它。@Hal:您不能。IntPtr.Size表示32/64位;它是一个参考的大小。