Java and the exact reference size for objects, arrays, and primitive types

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

I will try to explain in the following example: using a 64-bit JVM, the size of the pointer should be 8 bytes, therefore:

  • Object singletest = new Object(); will take 8 bytes to refer to the object plus the size of the object
  • Object arraytest = new Object[10]; will take 8 bytes to refer to 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; takes only 2 bytes, because int is a primitive type
  • int[] arrayint = new int[10]; will take 8 bytes for position reference and 10 * 2 bytes for elements

In addition, it is for this reason that Java allows you to write code as follows:

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

, , (aka pointer), , ( , ). : doublearray (8 ), - , 8 * 2 ( ) , , 2 * 5 2 * 10.

, , :

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;}
}

, ( , Java) 8 2 + 2 .

: , ? , , , 8 ? , ?

, , ( "int i", ++, , ​​ "0" ).

... , , ! (, , , )

+5
1

64- JVM, 8 ,

32-, 32 . , Java , ( 8-, 1- )

JVM , JVM .

Object singletest = new Object(); 8 Object

16 . 4 , .

Object arraytest = new Object [10];

16 , 10 ( 56 )

int singleint = new int; 2 , int

int 32- , new. , 4 , .

int [] arrayint = new int [10]; 8 10 * 2

, new Object[10] (56 )

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

, double[]

16 + 2 * 4 doublearray 16 + 5 * 4 + 4 ( ) 16 + 10 * 4.

, , 8- .

, ( , Java) 8 2 + 2 .

.

16 , int 2 * 4 .

,

Java , .

, ?

, .

"int i", ++, , ​​ "0"

, , (, 4 ). , JIT , .

, ,

... .;)

+8

All Articles