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>.
-
3And stackoverflow.com/questions/207592/…Jon Skeet– Jon Skeet2009-03-03 09:14:23 +00:00Commented Mar 3, 2009 at 9:14
-
1Any container is a relatively small object that holds a reference to some data storage (usually an array) outside the actual container object - and that in turn holds references to the actual objects you added to the container. So the question how much memory a List takes is not even well defined - the size of the list object itself, memory allocated by the list object, total size for everything in the list and the amount of memory that will be freed when the list is collected are all different values.Nir– Nir2009-03-03 09:17:00 +00:00Commented Mar 3, 2009 at 9:17
-
and stackoverflow.com/questions/555929/c-memory-usage-of-an-object/…Pete Kirkham– Pete Kirkham2009-03-03 10:09:26 +00:00Commented Mar 3, 2009 at 10:09
-
See the benchmarks in the test app I have created: github.com/scholtz/TestDotNetCollectionsMemoryAllocationScholtz– Scholtz2019-04-16 09:39:27 +00:00Commented Apr 16, 2019 at 9:39
-
1If you are using Visual Studio, you can use the new .NET Object Allocation Tool: devblogs.microsoft.com/visualstudio/…Ricky Han– Ricky Han2022-06-21 00:54:06 +00:00Commented Jun 21, 2022 at 0:54
6 Answers
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;
}
22 Comments
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 Find out how much memory is being used by an object in C#? for a similar question.
3 Comments
Unmanaged object:
Marshal.SizeOf(object yourObj);
Value Types:
sizeof(object val)
Managed object:
- Looks like there is no direct way to get for managed objects, Ref: https://learn.microsoft.com/en-us/archive/blogs/cbrumme/size-of-a-managed-object
3 Comments
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.
2 Comments
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;
}
5 Comments
GC.GetTotalMemory approach.In debug mode
load SOS
and execute dumpheap command.