Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference type size in java

Tags:

java

Why does a reference type in java take 8 bytes? Why not less or more than 8 bytes?

like image 805
manoj Avatar asked Mar 18 '11 10:03

manoj


3 Answers

Actually, it is nowhere specified how much bytes a reference variable shall have, and in fact, it is not everywhere the same.

Common virtual machines for 32-bit systems (i.e. systems with 32 bit adresses in the memory bus) usually use 32-bit (= 4 bytes, same as int and float) as the size for object references, while virtual machines for 64-bit systems often use the native address size 64 bits (= 8 bytes) for these. (Note that most 64 bit systems can run 32-bit programs, too, so often even there you'll be using a 32 bit VM.)

It is simply a matter of simplifying the implementation, if you can use an actual memory address for your references instead of something else.

As this increases the size of memory used (and often we don't actually need to access that much memory), from Java 7 on the 64-bit HotSpot VM will be able to use 32-bit references under certain conditions, i.e. when the heap is smaller than 32 GB (8·232 bytes). To convert them to a actual memory address, they are multiplied by 8 (as objects will be aligned on 8-byte-boundaries) and then added to the base address (if this is not zero). (For heaps smaller than 4 GB, we don't need the multiplication step.)

Other VMs might use similar tricks.

like image 64
Paŭlo Ebermann Avatar answered Oct 12 '22 08:10

Paŭlo Ebermann


This is because a reference variable doesn't actually hold the object. It's a way to reach the object. The way JVM manages this is something of our least concern. You can think of it as an address to the location of the object in the heap. But it need not be as straight forward as an address.

like image 34
adarshr Avatar answered Oct 12 '22 07:10

adarshr


Let me give an example to help you understand better

    String myName = new String("John");
    String yourName = new String("John");
    if (myName == yourName) 
    {
        System.out.println("Refereces point to Same objects on heap");
    } else 
    {
        System.out.println("Refereces point to different objects on heap");
    }

and output is Refereces point to different objects on heap.

Here myName and yourName are two references which point to objects of type String(memory allocated on Heap). Note memory for reference variables is allocated on stack and not on the Heap. Both the reference variables merely have the address(or similar unique abstraction) of the String object which resides on the Heap. The size of address will be constant for a particular architecture of OS. In case of 32 bits it will be 4 bytes where as for 64 bits it will be 8 bytes.

So size of objects pointed be the reverence variable may change but the size of references remain the same as they simply carry address which remains constant for any particular architecture.

like image 3
Aniket Thakur Avatar answered Oct 12 '22 07:10

Aniket Thakur