• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory allocated to reference variables  RSS feed

 
vinodh rmahen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted some clarification regarding the amount of memory allotted to object reference variables.
Do, reference variables to different kinds of Objects occupy the same amount of memory (say 4 bytes) ex:



I assumed that these object reference variables store the memory address of their corresponding objects. I wanted to know if these 3 variables [a,b,c] occupy the same amount of memory (say 4 bytes)
OR
these variables occupy different amounts depending on the type of Object being referred to.
If the later is true, are different types of Objects(String, Integer, Dog) allotted space at pre-determined sections in the Heap.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Yes, a reference takes always the same amount of memory. On 32bit Sun's VM it's 4 bytes, I do not know if it's the same on 64bit VMs. You can have a look at the VM spec for more details. And no, there is no "heap space per class" - classes can be loaded dynamically, so the VM cannot determine which classes are available when. I don't think it would make much sense anyway, would it?
 
vinodh rmahen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Michalik wrote:Yes, a reference takes always the same amount of memory.

Do references store only the memory address of the Object they are pointing to, if so then where is the information about the size of the Object they are referring to available?
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tough question and I think it's up to VM's implementer to answer it. I remember reading that in early Sun Java VM versions the references were indirect - they did not store direct pointers to the memory, but to some map that was mapping 'virtual' references to 'real' memory pointers. Now they are direct - see Hotspot whitepaper. But again, a reference itself does not store the information on object's identity. It is stored "somewhere else". Don't ask me where ;) It's just another data structure of the VM. If you're interested these links may be helpful:
Sun Hotspot VM
Hotspot Garbage Collector
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Information about the size would be obtainable from the .class file. I don't know whether it is recorded explicitly or whether you would have to calculate it afresh every time by counting fields. A few objects (eg arrays) carry their size with them (= the length field).

The whole idea of using a high-level language like Java is to hide that sort of low-level implementation details.
 
vinodh rmahen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!