内存中的System.Collections.Generic.List <T>大小

hal*_*000 2 .net memory-management

只是想知道是否有人试图确定内存中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; 
} 
Run Code Online (Sandbox Code Playgroud)

这适用于其他人,但不适用于我需要的东西.

SLa*_*aks 7

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

A List<T>将有一些开销(两个ints)和一个大小的数组Capacity.

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

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