Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much space does a reference take?

Tags:

c#

I'm not even sure if I'm using the right terms here, but if I have a code like this: (in C#)

Object object1;
Object object2;
object1 = new Object();

object2 = object1;

Will object2 take up as much space as object1, or just point to the instance created at object1?

If I'm not using the correct terms to express myself properly, by all means tell me as well.

like image 215
user1974555 Avatar asked Jan 11 '23 23:01

user1974555


1 Answers

A reference takes exactly one word of memory. That's 32 bits in a 32 bit application, 64 bits in a 64 bit application, etc.

Both variables take up exactly one word of memory, since they are of reference types. There is also some amount of memory, somewhere, that holds however much is needed for one actual object instance. Both of those variables in your program happen to contain a reference to that object, but they'd take up the same amount of space even if they didn't. (A null reference takes up the same space as a valid reference, after all.)

like image 163
Servy Avatar answered Jan 17 '23 15:01

Servy