<p>Does anyone know of a way to find out how much memory an instance of an object is taking?</p>

<p>For example, if I have an instance of the following object:</p>

<pre><code>TestClass tc = new TestClass();
</code></pre>

<p>Is there a way to find out how much memory the instance <code>tc</code> is taking?</p>

<p>The reason for asking, is that although C# has built in memory management, I often run into issues with not clearing an instance of an object (e.g. a List that keeps track of something).</p>

<p>There are couple of reasonably good memory profilers (e.g. ANTS Profiler) but in a multi-threaded environment is pretty hard to figure out what belongs where, even with those tools. </p>

[{'answer_id': 60829, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 2, 'selected': False, 'text': '<p>I have good experiences with <a href="http://memprofiler.com/" rel="nofollow noreferrer">MemProfiler</a>. It gives you stack traces of when the object was created and all the graphs of why the object is still not garbage collected.</p>\n'}, {'answer_id': 60834, 'author': 'Alex Duggleby', 'author_id': 5790, 'author_profile': 'https://Stackoverflow.com/users/5790', 'pm_score': 3, 'selected': False, 'text': '<p>If you are not trying to do it in code itself, which I\'m assuming based on your ANTS reference, try taking a look at CLRProfiler (currently v2.0). It\'s free and if you don\'t mind the rather simplistic UI, it can provide valuable information. It will give you a in-depth overview of all kinds of stats. I used it a while back as one tool for finding a memory leek.</p>\n<p>Download here: <a href="https://github.com/MicrosoftArchive/clrprofiler" rel="nofollow noreferrer">https://github.com/MicrosoftArchive/clrprofiler</a></p>\n<p>If you do want to do it in code, the CLR has profiling APIs you could use. If you find the information in CLRProfiler, since it uses those APIs, you should be able to do it in code too. More info here:\n<a href="http://msdn.microsoft.com/de-de/magazine/cc300553(en-us).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/de-de/magazine/cc300553(en-us).aspx</a></p>\n<p>(It\'s not as cryptic as using WinDbg, but be prepared to do mighty deep into the CLR.)</p>\n'}, {'answer_id': 60837, 'author': 'Scott Saad', 'author_id': 4916, 'author_profile': 'https://Stackoverflow.com/users/4916', 'pm_score': 2, 'selected': False, 'text': '<p>The <a href="http://www.microsoft.com/downloads/details.aspx?familyid=a362781c-3870-43be-8926-862b40aa0cd0&amp;displaylang=en" rel="nofollow noreferrer">CLR Profiler</a>, which is provide free by Microsoft does a very good job at this type of thing. </p>\n\n<p>An introduction to the whole profiler can be downloaded <a href="http://www.microsoft.com/downloads/details.aspx?familyid=CD5AA57C-9CD1-45AB-AA4B-8DC586D30938&amp;displaylang=en" rel="nofollow noreferrer">here</a>. Also the Patterns &amp; Practices team <a href="http://msdn.microsoft.com/en-us/library/ms979205.aspx" rel="nofollow noreferrer">put something</a> together a while back detailing how to use the profiler.</p>\n\n<p>It does a fairly reasonable job at showing you the different threads and objects created in those threads.</p>\n\n<p>Hope this sheds some light. Happy profiling!</p>\n'}]
['https://Stackoverflow.com/questions/60820', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6301/']