Silly question, but in a winforms app Im currently working on, I would like to get the amount of bytes allocated/used by a List<[SomeObject]>
held in memory (for statistical purposes). Is this possible? I have searched thru the possible options, but there is obviously no myList.GetTotalBytes()
method.
This may be a full-of-horse-pucky answer, but I'm going to go out on a limb and say if you're doing statistical comparisons, do a binary serialize of the object to a MemoryStream
and then look at its Length
property as such:
List<string> list = new List<string>
{
"This",
"is",
"a",
"test"
};
using (Stream stream = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, list);
Console.WriteLine(stream.Length);
}
Take note that this could change between different versions of the framework and would only be useful for comparisons between object graphs within a single program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With