CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    357

    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?

  2. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    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.

  3. #3
    Join Date
    Nov 2006
    Posts
    357

    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?

  4. #4
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: How much memory does a reference to a reference type take up?

    Quote Originally Posted by Grofit View Post
    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

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    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.

  6. #6
    Join Date
    Nov 2006
    Posts
    357

    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
  •  





Click Here to Expand Forum to Full Width

Featured