Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and exact reference size for objects, array and primitive types

I would like to know exactly the real space allocated in memory for an object.

I try to explain with some example: using a 64 bit JVM, pointer size should be 8 bytes, so:

  • Object singletest = new Object(); will take 8 bytes to reference the Object plus the size of the Object
  • Object arraytest = new Object[10]; will take 8 byte to reference the position where the array is stored plus 8*10 bytes to store the array plus the size of each Object
  • int singleint = new int; will take just 2 bytes, because int is a primitive type
  • int[] arrayint = new int[10]; will take 8 bytes to reference the position and 10*2 bytes for the elements

Moreover, this is the reason why Java allows to write code like this:

int[][] doublearray = new int[2][];
int[0][] = new int[5];
int[1][] = new int[10];

What really happen is that an array will produce a reference (aka pointer) like an object, so it doesn't really matter the size of the second dimension at declaration time (and dimensions can be different, there is no link between them). Then the space taken will be: a reference to doublearray (8 bytes), first dimension is simply a reference to the second one, so other 8 bytes * 2 (first dimension size), and finally 2 bytes * 5 plus 2 bytes * 10.

So, finally, if have a real class like this:

class Test {
   int a, b;
   int getA() {return A};
   void setA(int a) {this.a = a;}
   int getB() {return B};
   void setB(int b) {this.b = b;}
}

when I call a new to instantiate, a pointer (or name it reference, because it's Java) of 8 bytes will be used plus 2+2bytes to store the integers into the class.

The questions are: am I right or I wrote total nonsense? Moreover, when I don't instantiate an object but I just declare it, 8 bytes will be allocated for further use or not? And what if I assign a null value?

Meanwhile for primitive type I'm quite sure that just declaring it will allocate the requested space (if I declare an "int i" then I can immediately call i++ because no reference are used, just a portion of memory is setted on "0").

I searched on internet without clever response... I know that I wrote a lot of questions, but any help will be appreciated! (and maybe I'm not the only one interested)

like image 920
pierpytom Avatar asked Apr 30 '12 15:04

pierpytom


People also ask

What is the size of reference in Java?

References have a typical size of 4 bytes on 32-bit platforms and on 64-bits platforms with heap boundary less than 32Gb (-Xmx32G), and 8 bytes for this boundary above 32Gb. This means that a 64-bit JVM usually requires 30-50% more heap space.

What is the difference between primitive type and reference type in Java?

Variables in Java are classified into primitive and reference variables. From the programmer's perspective, a primitive variable's information is stored as the value of that variable, whereas a reference variable holds a reference to information related to that variable.

Are arrays objects reference types in Java?

In addition, array types in Java are reference types because Java treats arrays as objects. The two main characteristics of objects in Java are that: Objects are always dynamically allocated.

What are the differences between Java primitives and Java primitives objects?

Primitives are passed by value, i.e. a copy of the primitive itself is passed. Whereas for objects, the copy of the reference is passed, not the object itself. Primitives are independent data types, i.e. there does not exist a hierarchy/super class for them. Whereas every Object is descendent of class "Object".


1 Answers

using a 64 bit JVM, pointer size should be 8 bytes,

Actually its usually 32-bit unless you have a maximum heap size of 32 GB or more heap. This is because Java uses references, not pointers (and each object is on an 8 byte, not 1 boundary)

The JVM can change the size of a reference depending on which JVM you use and what the maximum heap size is.

Object singletest = new Object(); will take 8 bytes to reference the Object plus the size of the Object

The object will use about 16 bytes of heap. It may or may not use 4 bytes of stack, but it could just use a register.

Object arraytest = new Object[10];

This will use about 16 bytes for the header, plus 10 times the reference sizes (about 56 bytes in total)

int singleint = new int; will take just 2 bytes, because int is a primitive type

int is always 32-bit bit, you can't create a newprimitive. As its notionally on the stack it might use 4-bytes of stack or it might only use a register.

int[] arrayint = new int[10]; will take 8 bytes to reference the position and 10*2 bytes for the elements

Again the object is likely to be the same size as the new Object[10] (56 bytes)

int[][] doublearray = new int[2][];
int[0][] = new int[5];
int[1][] = new int[10];

I wouldn't call it a doublearray as it could be confused with double[]

However the size is likkely to be about 16 + 2 * 4 for doublearray and 16 + 5*4 + 4 (for padding) and 16 + 10 * 4.

Memory allocated on the heap is aligned to an 8 byte boundary.

I call a new to instantiate, a pointer (or name it reference, because it's Java) of 8 bytes will be used plus 2+2bytes to store the integers into the class.

The reference is on the stack and usually this is not included.

The object has a header of about 16 bytes and the int values take up 2 * 4 bytes.

when I don't instantiate an object but I just declare it

Java doesn't let you declare Objects, only primitives and references.

what if I assign a null value?

That could change the value of the reference, but otherwise nothing happens.

if I declare an "int i" then I can immediately call i++ because no reference are used, just a portion of memory is setted on "0"

No heap will be used, possibly no stack will be used (possibly 4 bytes). Possible the JIT compiler will remove the code if it doesn't do anything.

maybe I'm not the only one interested

... but was not too afraid to ask. ;)

like image 92
Peter Lawrey Avatar answered Oct 07 '22 11:10

Peter Lawrey