<p>I'm looking for a way to get the size of an instance of a reference type. sizeof is only for value types. Is this possible?</p>

[{'answer_id': 26581, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 4, 'selected': False, 'text': '<p>You need <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.sizeof.aspx" rel="noreferrer">Marshal.SizeOf</a></p>\n\n<p><strong>Edit:</strong> This <em>is</em> for unsafe code, but then, so is sizeof().</p>\n'}, {'answer_id': 26590, 'author': 'juan', 'author_id': 1782, 'author_profile': 'https://Stackoverflow.com/users/1782', 'pm_score': 1, 'selected': False, 'text': "<p>Beware that Marshal.SizeOf is for unsafe code...</p>\n\n<p>I don't think it's possible for managed code though, maybe you can explain your problem, there may be another way to solve it</p>\n"}, {'answer_id': 26600, 'author': 'juan', 'author_id': 1782, 'author_profile': 'https://Stackoverflow.com/users/1782', 'pm_score': 4, 'selected': True, 'text': "<p>If you don't mind it being a little less accurate than perfect, and for comparative purposes, you could serialize the object/s and measure that (in bytes for example)</p>\n\n<p><em>EDIT (I kept thinking after posting)</em>: Because it's a little more complicated than sizeof for valuetypes, for example: reference types can have references to other objects and so on... there's not an exact and easy way to do it that I know of...</p>\n"}, {'answer_id': 6580593, 'author': 'serhio', 'author_id': 185593, 'author_profile': 'https://Stackoverflow.com/users/185593', 'pm_score': 1, 'selected': False, 'text': '<p>If you can - Serialize it!</p>\n\n<pre><code>Dim myObjectSize As Long\n\nDim ms As New IO.MemoryStream\nDim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()\nbf.Serialize(ms, myObject)\nmyObjectSize = ms.Position\n</code></pre>\n'}, {'answer_id': 6668193, 'author': 'GrantJ', 'author_id': 232571, 'author_profile': 'https://Stackoverflow.com/users/232571', 'pm_score': 2, 'selected': False, 'text': '<p>I had a similar question recently and wanted to know the size of Object and LinkedListNode in C#. To solve the problem, I developed a <a href="http://www.grantjenks.com/wiki/random/net_sizeof_reference_type" rel="nofollow noreferrer">program</a> that would:</p>\n\n<ol>\n<li>Measure the program\'s "Working Set"</li>\n<li>Allocate a lot of objects.</li>\n<li>Measure the "Working Set" again.</li>\n<li>Divide the difference by the number of allocated objects.</li>\n</ol>\n\n<p>On my computer (64-bit), I got the following data:</p>\n\n<pre><code>Measuring Object:\niter    working set     size estimate\n-1      11190272\n1000000 85995520        74.805248\n2000000 159186944       73.998336\n3000000 231473152       73.4276266666667\n4000000 306401280       73.802752\n5000000 379092992       73.580544\n6000000 451387392       73.3661866666667\n7000000 524378112       73.3125485714286\n8000000 600096768       73.613312\n9000000 676405248       73.9127751111111\nAverage size: 73.7577032239859\nMeasuring LinkedListNode&lt;Object&gt;:\niter    working set     size estimate\n-1      34168832\n1000000 147959808       113.790976\n2000000 268963840       117.397504\n3000000 387796992       117.876053333333\n4000000 507973632       118.4512\n5000000 628379648       118.8421632\n6000000 748834816       119.110997333333\n7000000 869265408       119.299510857143\n8000000 993509376       119.917568\n9000000 1114038272      119.985493333333\nAverage size: 118.296829561905\nEstimated Object size: 29.218576886067\nEstimated LinkedListNode&lt;reference type&gt; size: 44.5391263379189\n</code></pre>\n\n<p>Based on the data, the average size of allocating millions of Objects is approximately 29.2 bytes. A LinkedListNode object is approximately 44.5 bytes. This data illustrates two things:</p>\n\n<ol>\n<li>It\'s very unlikely that the system is allocating a partial byte. The fractional measure of bytes indicates the overhead the CLR requires to allocate and track millions of reference types.</li>\n<li>If we simply round-down the number of bytes, we\'re still unlikely to have the proper byte count for reference types. This is clear from the measure of Objects. If we round down, we assume the size is 29 bytes which, while theoretically possible, is unlikely because of padding. In order to improve performance, object allocations are usually padded for alignment purposes. I would guess that CLR objects will be 4 byte aligned.</li>\n</ol>\n\n<p>Assuming CLR overhead and 4-byte alignment, I\'d estimate an Object in C# is 28 bytes and a LinkedListNode is 44 bytes.</p>\n\n<p>BTW Jon Skeet had the idea for the method above before I did and stated it in <a href="https://stackoverflow.com/questions/207592/getting-the-size-of-a-field-in-bytes-with-c#207605">this answer</a> to a similar question.</p>\n'}, {'answer_id': 7122728, 'author': 'Gomes', 'author_id': 902539, 'author_profile': 'https://Stackoverflow.com/users/902539', 'pm_score': 1, 'selected': False, 'text': '<p>Please refer my answer in the below link.</p>\n\n<p>It is possible via .sos.dll debugger extension</p>\n\n<p><a href="https://stackoverflow.com/questions/324053/find-out-the-size-of-a-net-object/7122653#7122653">Find out the size of a .net object</a></p>\n'}]
['https://Stackoverflow.com/questions/26570', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1786/']