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 int
s), 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.
精彩评论