Get byte size of List<T>

.net, byte, list, winforms

Solution

I'm not sure the runtime provides a reliable programmatic method of getting an object's size, however there are some options open to you:

- use a tool like CLR Profiler

- use Marshal.SizeOf() (returns the unmanaged size of the object)

- serialize your object to binary for an approximation

Problem

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.

Original source