Get the size of an object in heap memory

I posted about this before here. However the methods I’ve been using haven’t worked so far. What I want to achieve is the ability to calculate the size of an object in heap memory, equal to that of the “!ObjSize” command in SOS (WinDbg).

What I’ve tried:

  • dotnetex: Doesn’t work and it’s outdated.

  • ObjectLayoutInspector: Seems promising but I’m not sure how accurate it is.

  • Marshal.ReadInt32(typeof(T).TypeHandle.Value, 4): I believe this is the base instance size of a type, but I’m not sure.

All of these methods seem to give varying results, either because they’re inaccurate or because they measure size in different ways. Regardless, I want to determine the size of an object equal to Son of Strike’s “!ObjSize” command at runtime.

submitted by /u/_Decimation
[link] [comments]

Leave a Reply