Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof() equivalent for reference types?

Tags:

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?

like image 885
John Sheehan Avatar asked Aug 25 '08 18:08

John Sheehan


People also ask

What is reference size?

Separation size, designated size, or control size used to define analyses of the products of a sizing operation.

Are all references the same size?

It depends on OS version in windows. The size of the reference itself will depend on your processor architecture - 4 bytes on 32-bit, 8 bytes on 64-bit. The reference itself is basically a pointer. 32 bits on a 32 bit OS, 64 bits on a 64 bit OS.

How to find size of data types in c#?

sizeof() Operator in C# The sizeof() operator is used to obtain the size of a data type in bytes in bytes. It will not return the size of the variables or instances. Its return type is always int.

How many bytes does a reference take C++?

why reference size is always 4 bytes - c++ - Stack Overflow.


2 Answers

You need Marshal.SizeOf

Edit: This is for unsafe code, but then, so is sizeof().

like image 171
Greg Hurlman Avatar answered Sep 23 '22 19:09

Greg Hurlman


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)

EDIT (I kept thinking after posting): 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...

like image 26
juan Avatar answered Sep 24 '22 19:09

juan