-
July 10th, 2009, 06:02 AM
#1
How much memory does a reference to a reference type take up?
Hey,
Kinda a weird one i know, but to keep the question simple lets say you have a class that you made, and you have a list of them. Then you need to make objects that reference that one object...
So in this scenario there is 1 reference type allocated in memory that contains some bits and bobs, then there are lets say 10000 other objects that reference the 1 allocated object, although the object is already in memory, surely the other objects have to allocate a bit of space to know what address they are pointing to...
Code:
public class AllocatedClass
{
public int SomeVar;
}
public class ReferencerClass
{
protected AllocatedClass AllocatedClassLookup;
public ReferencerClass(AllocatedClass Lookup) { AllocatedClassLookup = Lookup; }
}
AllocatedClass Allocated = new AllocatedClass();
List<ReferencerClass> ReferenceList = new List<ReferencerClass>();
for(uint i =0; i<uint.Max;i++)
{ ReferenceList.Add( new ReferencerClass(Allocated) ); }
That above example is pretty much what im talking about, there would be 1 allocation of the first class and all the others would only have a reference to that variable, although its an immutable reference how much space does each one take up roughly?
-
July 10th, 2009, 06:27 AM
#2
Re: How much memory does a reference to a reference type take up?
If I recall correct it should usually be 4 byte on 32bit and 8 byte on a 64 bit, as it is a number pointer to a memory location.
I'm not 100% sure though.
-
July 10th, 2009, 06:28 AM
#3
Re: How much memory does a reference to a reference type take up?
I thought it would probably be something like that as its just going to be an address lookup right?
-
July 10th, 2009, 06:50 AM
#4
Re: How much memory does a reference to a reference type take up?
Originally Posted by Grofit
I thought it would probably be something like that as its just going to be an address lookup right?
Yes - a reference itself should just be taking the space it needs to hold the memory address. Which is why I recall it as 4/8 depending on 32/64bit
-
July 11th, 2009, 04:15 PM
#5
Re: How much memory does a reference to a reference type take up?
You're actually confusing a few things here In .NET you can't store a pointer to an object, so there is no way to store a 'reference to a reference'.
Every class you instantiate has 12 bytes of associated overhead. 4 bytes for type information, 4 bytes for the sync block (so you can use the 'lock' keyword) and 4 bytes i can't remember. That's the absolute minimum size a .NET object can be.
Code:
public class ReferencerClass
{
protected AllocatedClass AllocatedClassLookup;
public ReferencerClass(AllocatedClass Lookup) { AllocatedClassLookup = Lookup; }
}
This would be 12 + size (void*) bytes in size, as the field is essentially storing a pointer to another object. So on a 32bit system that would make each instance of your 'Referencer' class be 16 bytes.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
July 13th, 2009, 04:10 AM
#6
Re: How much memory does a reference to a reference type take up?
Ah brilliant, thanks for clearing it up... when i say pointer i mean the logic of a pointer... i read the address is not a reference article previously, but its just hard to get away from C++ naming conventions...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|