The amount of memory of member variables in a Java class

I am trying to understand the memory area of ​​the java class, which uses various data members inside the class. I found some reference materials going through this article:

http://java-performance.info/overview-of-memory-saving-techniques-java/

Based on this, I understand that if I have a class with only one array of a primitive type, the declaration of the array itself should consume 12 bytes. The class header must take another 12 bytes. The total memory size of an object of type SingleArray will be 24 bytes.

public class SingleArray {
    int[] array1;
}

Using a toolkit of objects, I found that a SingleArray object really consumes 24 bytes without any data. In this regard, an object of the DoubleArray class should consume 40 bytes (subject to addition):

public class DoubleArray {
    int[] array1;
    long[] array2;
}

DoubleArray, , 24 . . ?

+4
2

, , , - . jol-cli-0.3.2-full.jar http://openjdk.java.net/projects/code-tools/jol/

java -jar jol-cli-0.3.2-full.jar internals -cp bin x.SingleArray

SingleArray 64- Java HotSpot TM ( 25.66-b17, ):

 OFFSET  SIZE  TYPE DESCRIPTION     
      0     4       (object header) 
      4     4       (object header) 
      8     4       (object header) 
     12     4 int[] SingleArray.array1
Instance size: 16 bytes (reported by Instrumentation API)
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

DoubleArray:

 OFFSET  SIZE   TYPE DESCRIPTION                    VALUE
      0     4        (object header)
      4     4        (object header)
      8     4        (object header)
     12     4  int[] DoubleArray.array1
     16     4 long[] DoubleArray.array2
     20     4        (loss due to the next object alignment)
Instance size: 24 bytes (reported by Instrumentation API)
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

, . , .

1) , , , , 12 . SingleArray .

2) 4 8 . 64- 32 . . .

+5

, array declaration Class header , .

SingleArray DoubleArray? JVM, :

SingleArray: 16

  • 8
  • 4 array1
  • 4 , 8

DoubleArray: 16

  • 8
  • 4 array1
  • 4 array2
  • 0 , 8
+1

All Articles