Note that there are some explanatory texts on larger screens.

plurals
  1. POprogrammatically find memory used by object
    primarykey
    data
    text
    <p>Is there a way to programmatically and accurately determine the amount of memory used by an object in c#? I am not concerned with how slow the process is, so running GCs left and right is acceptable (of course I'd prefer something more efficient).</p> <ul> <li><p>Serializing objects and looking at the resulting length doesn't seem very accurate (in a simple test of this method, I saw that an integer returned a value of 54).</p></li> <li><p>Using <code>GC.GetTotalMemory(true)</code> seems to produce inconsistent values, not to mention they appear too large.</p></li> <li><p>Using <code>Marshal.SizeOf(object)</code> produces accurate results, but only appears to work with primitives.</p></li> </ul> <p>If nothing along those lines is available, an alternative would be to calculate sizes based on the structures used and the primitives involved. This would also be acceptable (though upsetting), but I'd need to know the correct method of calculating object overheads, etc. Any literature that would show me a way to do this would be awesome.</p> <p>Similar SO questions (none of which seemed to have concrete methods for accurate calculation of object size):</p> <p><a href="https://stackoverflow.com/questions/426396/how-much-memory-does-a-c-net-object-use">How much memory does a C#/.NET object use?</a></p> <p><a href="https://stackoverflow.com/questions/750574/how-to-get-memory-available-or-used-in-c-sharp">How to get memory available or used in C#</a></p> <p><a href="https://stackoverflow.com/questions/605621/how-to-get-object-size-in-memory">How to get object size in memory?</a></p> <p><a href="https://stackoverflow.com/questions/26570/sizeof-equivalent-for-reference-types">sizeof() equivalent for reference types?</a></p> <p>Tools to profile memory (non-programmatic approach):</p> <p><a href="http://www.microsoft.com/en-us/download/details.aspx?id=16273" rel="nofollow noreferrer">http://www.microsoft.com/en-us/download/details.aspx?id=16273</a></p> <p><a href="https://stackoverflow.com/questions/60820/find-out-how-much-memory-is-being-used-by-an-object-in-c">Find out how much memory is being used by an object in C#?</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload