0

How much space in memory in bytes does reference data types in java consume? Basically how many bytes do variables of class type take up, similar to int data type consuming 4 bytes.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ashley
  • 57
  • 5
  • Why do you think reference would allocate space and not object itself? – SMA Oct 24 '15 at 06:14
  • 4
    Possible Duplicate of http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java – Rahman Oct 24 '15 at 06:15
  • Yeah as the link says, generally accepted answer is 8 bytes. I think it might be more on 64 bit run times though if you're using 64 bit pointers in the JVM. – markspace Oct 24 '15 at 06:21

1 Answers1

1

A reference variable is a pointer, and has the size of a pointer, so it depends on the JVM (32-bit or 64-bit), and whether pointers are compressed (compressedOop).

This is the space of the reference, not the Object potentially referenced by the variable (when not null).

Andreas
  • 154,647
  • 11
  • 152
  • 247