开发者

System.Collections.Generic.List<T> size in memory

开发者 https://www.devze.com 2023-02-13 00:01 出处:网络
Just wondering if anyone has ever tried to determine the actual size of a System.Collections.Generic.List in memory?

Just wondering if anyone has ever tried to determine the actual size of a System.Collections.Generic.List in memory?

I am currently caching one such object using System.HttpRuntime.Cache which seems to work well but I'd love to be able to determine the actual impact on my server.

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; 
开发者_如何学Go} 

This works for others but not for what I need.


The serialized size of the list will be totally irrelevant.

A List<T> will have a bit of overhead (two ints), and an array of size Capacity.

Arrays of reference types use IntPtr.Size (4 or 8) bytes of memory per element; arrays of value types use Marshal.SizeOf(type) bytes of memory per element.
(Arrays also have a bit of overhead)

Any reference types inside of the list (or in structs in the list) will use memory separately.

0

精彩评论

暂无评论...
验证码 换一张
取 消