1

I am trying to figure out which of the two objects require more memory.

By checking this reference now I see a null costs 4 to 8 bytes Java - Does null variable require space in memory

But no idea how much would cost to have an empty ArrayList of String. Does it cost the same talking about memory?

Any idea about it?

jpganz18
  • 5,508
  • 17
  • 66
  • 115
  • Easy way to find out: Create a billion of each. – tadman Mar 05 '18 at 17:55
  • 1
    Probably. My computer has many gigabytes (that's thousands of millions of bytes). I haven't cared about that kind of level of optimization in quite some time. – Elliott Frisch Mar 05 '18 at 17:56
  • 5
    Worry less about memory, and more about robustness. You should never treat null as equivalent to an empty collection or array. If code wants to indicate an empty collection, do it with an empty collection. – VGR Mar 05 '18 at 17:57
  • You should be specific if you're interested in the memory occupied by the reference or by the object. – lexicore Mar 05 '18 at 18:05

4 Answers4

10

If, by empty list, you mean:

List<Object> empty = new ArrayList<> ();

Then that will take a lot more space than using null.

But if you plan to use:

List<Object> empty = Collections.emptyList(); // I'm a singleton

Then because all your empty lists will refer to the same object, you will end up with the same memory consumption as if you were using null.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

You should use

Collections.emptyList()

Internally this points to a public static final List EMPTY_LIST = new EmptyList<>(); so won't eat any extra memory. It has the same effect as creating an empty list yourself, but without the overhead.

Martin Cassidy
  • 686
  • 1
  • 9
  • 28
1

Explicitly constructing a new empty list will use more memory. Most (if not all) implementations of List have some book keeping and overhead (size, head, tail, etc) that are incurred. eg. LinkedList has a tail and a head which is going to use up some more memory when instantiated.

Ultimately null will only use 4-8 bytes depending on whether you're running a 32 or 64 bit platform.

As mentioned in other answers, if you only ever need and empty list you should use Collections.emptyList(), however if you do need a mutable list you won't be able to use this as the instance you'll get back is immutable.

To answer the initial question about ArrayList, the implementation only has a single extra field other than the stored elements, size. So although it won't use as little space as null the overhead is miniscule.

TLDR; null will only use the space needed to store a 32 or 64 bit pointer, an actual instantiation of a list implementation will consume more memory because of some state that is held on the implementation.

Daniel
  • 589
  • 4
  • 19
0

The reference takes the same memory (4 bytes on 32-bit systems or 8 bytes on 64-bit systems).

The "object" takes more space in case of an empty list, because null itself does not consume any space.

lexicore
  • 42,748
  • 17
  • 132
  • 221