Reference type size in java

Why does the reference type in java take up 8 bytes? Why not less than 8 bytes?

+5
java
Mar 18 2018-11-11T00:
source share
5 answers

In fact, it is not indicated anywhere how many bytes the reference variable will have, and in fact it is not the same everywhere.

Shared virtual machines for 32-bit systems (i.e. systems with 32-bit memory bus addresses) typically use 32-bit (= 4 bytes, same as int and float ) as the size for object references, while how virtual machines for 64-bit systems often use their own address size of 64 bits (= 8 bytes). (Note that most 64-bit systems can also run 32-bit programs, so even there you will use a 32-bit virtual machine.)

It's just a matter of simplifying the implementation if you can use the actual memory address for your references, and not something else.

Since this increases the size of the memory used (and often we actually do not need to access such large memory), from Java 7 to 64-bit, HotSpot will be able to use 32-bit links under certain conditions, that is, when the heap is less than 32 GB (8 2 32 bytes). To convert them to the actual memory address, they are multiplied by 8 (since the objects will be aligned at 8-byte boundaries), and then added to the base address (if it's not zero). (For heaps smaller than 4 GB, we do not need a multiplication step.)

Other virtual machines may use similar tricks.

+9
Mar 18 '11 at 19:17
source share

This is because the reference variable does not actually hold the object. This is a way to reach an object. How the JVM manages this is something of our least concern. You can think of it as the address of the location of the object on the heap. But it does not have to be as direct as the address.

+4
Mar 18 2018-11-11T00:
source share

Let me give you an example to help you better understand.

  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 the Referee's conclusion points to different objects in the heap .

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

Thus, the size of the objects pointed to by the pointer can vary, but the size of the links remains the same as the simple address, which remains constant for any particular architecture.

+2
May 18 '13 at 11:32
source share

8 Bytes is the size of a long variable in Java and allows you to reference 1,01457092 ร— 10 ^ 19 links, which should be sufficient for most applications, and more than most hard drives can handle, given that each of these reference points also need to store data ...

Edit: After reading one of the other answers, I wanted to clarify that a link is just a pointer to data and does not contain data, Java architects decided to use 64-bit values โ€‹โ€‹to indicate links, since this gives you a huge address space.

0
Mar 18 2018-11-11T00:
source share

Type reference values โ€‹โ€‹can be thought of as pointers to objects. An 8-byte address must be specified on the heap.

Read more about Java Virtual Machine Structure

0
Mar 18 2018-11-11T00:
source share



All Articles