Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure the amount of memory an individual object takes in .NET

I'm wondering if there is a simple command or instruction in C#/.NET and/or Visual Studio that can tell me how much memory an individual object is taking up? I have a sneaking suspicion that the sizeof() operator is going to lie to me ... am I justified in this belief?

There is a somewhat related question here, but no definitive answer is given on how to measure an individual object

like image 679
Jeffrey Cameron Avatar asked Aug 20 '10 18:08

Jeffrey Cameron


1 Answers

There is no definitive way because it's not simple for just any type of object.

What if that object contains references to other objects? What if those other objects have other objects referencing them? Which object actually owns that memory space? Is it the one that created it or the last one to touch it? At any one point it could have different owners. Or do you just care about how much space the reference takes?

There is also a ton of questions that have asked this as well... a quick search turns up:

How to get object size in memory?

C#: Memory usage of an object

Find out the size of a .net object

How much memory does a C#/.NET object use?

and the list goes on an on...

like image 154
Kelsey Avatar answered Oct 28 '22 17:10

Kelsey