How to get object size in memory?

C#.NetPerformanceMemoryProfiling

C# Problem Overview


I need to know how much bytes my object consumes in memory (in C#). for example how much my Hashtable, or SortedList, or List<String>.

C# Solutions


Solution 1 - C#

this may not be accurate but its close enough for me

long size = 0;
object o = new object();
using (Stream s = new MemoryStream()) {
	BinaryFormatter formatter = new BinaryFormatter();
	formatter.Serialize(s, o);
	size = s.Length;
}

Solution 2 - C#

I don't think you can get it directly, but there are a few ways to find it indirectly.

One way is to use the GC.GetTotalMemory method to measure the amount of memory used before and after creating your object. This won't be perfect, but as long as you control the rest of the application you may get the information you are interested in.

Apart from that you can use a profiler to get the information or you could use the profiling api to get the information in code. But that won't be easy to use I think.

See https://stackoverflow.com/questions/60820/find-out-how-much-memory-is-being-used-by-an-object-in-c for a similar question.

Solution 3 - C#

Unmanaged object:

  • Marshal.SizeOf(object yourObj);

Value Types:

  • sizeof(object val)

Managed object:

Solution 4 - C#

OK, this question has been answered and answer accepted but someone asked me to put my answer so there you go.

First of all, it is not possible to say for sure. It is an internal implementation detail and not documented. However, based on the objects included in the other object. Now, how do we calculate the memory requirement for our cached objects?

I had previously touched this subject in this article:

> Now, how do we calculate the memory requirement for our cached > objects? Well, as most of you would know, Int32 and float are four > bytes, double and DateTime 8 bytes, char is actually two bytes (not > one byte), and so on. String is a bit more complex, 2*(n+1), where n > is the length of the string. For objects, it will depend on their > members: just sum up the memory requirement of all its members, > remembering all object references are simply 4 byte pointers on a 32 > bit box. Now, this is actually not quite true, we have not taken care > of the overhead of each object in the heap. I am not sure if you need > to be concerned about this, but I suppose, if you will be using lots > of small objects, you would have to take the overhead into > consideration. Each heap object costs as much as its primitive types, > plus four bytes for object references (on a 32 bit machine, although > BizTalk runs 32 bit on 64 bit machines as well), plus 4 bytes for the > type object pointer, and I think 4 bytes for the sync block index. Why > is this additional overhead important? Well, let’s imagine we have a > class with two Int32 members; in this case, the memory requirement is > 16 bytes and not 8.

Solution 5 - C#

The following code fragment should return the size in bytes of any object passed to it, so long as it can be serialized. I got this from a colleague at Quixant to resolve a problem of writing to SRAM on a gaming platform. Hope it helps out. Credit and thanks to Carlo Vittuci.

/// <summary>
/// Calculates the lenght in bytes of an object 
/// and returns the size 
/// </summary>
/// <param name="TestObject"></param>
/// <returns></returns>
private int GetObjectSize(object TestObject)
{
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    byte[] Array;
    bf.Serialize(ms, TestObject);
    Array = ms.ToArray();
    return Array.Length;
}

Solution 6 - C#

In debug mode

load SOS

and execute dumpheap command.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLukas ŠalkauskasView Question on Stackoverflow
Solution 1 - C#Rush FrisbyView Answer on Stackoverflow
Solution 2 - C#Rune GrimstadView Answer on Stackoverflow
Solution 3 - C#NileshChauhanView Answer on Stackoverflow
Solution 4 - C#AliostadView Answer on Stackoverflow
Solution 5 - C#Kevin HirstView Answer on Stackoverflow
Solution 6 - C#A.m.a.LView Answer on Stackoverflow