Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much memory does a Java object use when all its members are null?

Is it correct to assume a Java object only takes up the 8 bytes for the object reference as long as all it's members are set to null or does the definition of members already use up space in the instance for some reason?

In other words, if I have a large collection of objects that I want to be space efficient, can I count on leaving unused members set to null for reducing memory footprint?

like image 569
Hanno Fietz Avatar asked Sep 18 '09 12:09

Hanno Fietz


2 Answers

No, you need either 4 or 8 bytes ( depending whether it's a 32 or 64 bit system ) for each null you are storing in a field. How would the object know its field was null if there wasn't something stored somewhere to tell it so?

like image 114
Pete Kirkham Avatar answered Sep 20 '22 10:09

Pete Kirkham


No, null is also information and has also to be stored.

like image 20
Toad Avatar answered Sep 19 '22 10:09

Toad