I am currently developing a data structure in which I am trying to minimize memory consumption. I have several instance variables that can be null depending on the placement of Node in Trie. I started to go down the path of creating separate classes (one that has an instance variable and the other doesn't), so I won’t waste a ton of space with null references ... but then I started to wonder how jvm works. still up to 8 bytes (provided that the x64-arch), if the reference to the object is zero or does it have a more optimal way to store the zero reference?
I am pretty sure that the JVM will use as much link space nullas any other. If you have an object with three reference fields, and you exclude it from the middle, I don’t think any virtual machine can move the 3rd one and save 4 or 8 bytes (and then when you change nullto something else , he will have to move things again). If it were technically feasible, it would not be worth it - the extra computational cost and code complexity will kill any potential benefits. In addition, partly due to C's legacy, on most machines, a pointer of bit 0 works as NULL at a very low level, so the link nullhas a pretty obvious idea.
null
Oracle/Sun JDK - XX: + UseCompressedOops , 4 8 64- 16 ( , null ).
: yes - . Java null - , . , . . 4 32- 8 64- .
.
, FlyWeight
(: , factory ).
, . .
- ( ). , ... FlyWeight.
: , int, 4 , undefined ( , , undefined). , , 4 . , 4 .
int
, . , , 4 8 ( JVM , -XX:+UseCompressedOops) . 4- 16 ( 16 32-, 64- Java- , 8 12 4 , 8 ).
-XX:+UseCompressedOops
, , 4 . int , 20 24 , ; 4 8 , null.