0

I was asked this question... ArrayList of Integer vs ArrayList of String - both storing values from 0 to 9 ... which takes more memory?

Oleksandr Pyrohov
  • 14,685
  • 6
  • 61
  • 90
Anuj
  • 51
  • 2

2 Answers2

3

Assuming lists are created as follows:

    List<Integer> integers = new ArrayList<>();
    integers.add(0);
    integers.add(1);
    // ...
    List<String> strings = new ArrayList<>();
    strings.add("0");
    strings.add("1");
    // ...

There two levels of answering this question.

First is about knowing memory consumption of Integer and String.

So, a String with one character takes 40 bytes (32 on Java 8). An Integer takes 16 bytes.

On this level, list of strings takes more memory than list of integers.

On the second level you have to know that string literals are interned and boxing an int with Integer.valueOf uses cache for values between -128 and at least 127. So basically both add("0") and add(0) will use cached objects and one could say that no additional memory is used.

So depending on whether you consider string pool/integer cache, the answer is either "list of strings takes more memory" or "both lists take the same amount of memory".

If this is an interview question, you should probably give both answers.

ps. I personally wouldn't have known the size of String or Integer out of the head, but would have guessed String to take more memory.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks @lexicore for such a detailed explanation. I don't know why people down voted my question. Not everyone knows everything. – Anuj Apr 29 '18 at 11:56
  • 1
    @AnujTela Your question shows lack of research. Googling "java size of integer" or "java size of string" returns links to existing questions like I've found. I think this is why your question is downvoted. – lexicore Apr 29 '18 at 11:59
  • @AnujTela If you feel that my answer helped you, you could upvote and/or [accept my answer](http://meta.stackexchange.com/a/5235). – lexicore Apr 29 '18 at 11:59
  • I did google to find it out but could get the kind of explanation you have provided @lexicore. Never mind will take care hence forth. – Anuj Apr 29 '18 at 12:06
  • A little bit weird. I use the tool in my answer to measure a single characher string, and got 48 bit(jdk9). – xingbin Apr 29 '18 at 12:26
1

Here is a test result using this tool, it shows that ArrayList<Integer> occupies less memory:

public static void main(String[] args) {
    ArrayList<Integer> integerArrayList = new ArrayList<>();
    ArrayList<String> stringArrayList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        integerArrayList.add(i);
        stringArrayList.add(String.valueOf(i));
    }
    System.out.println(RamUsageEstimator.sizeOf(integerArrayList));  // 240
    System.out.println(RamUsageEstimator.sizeOf(stringArrayList));  // 560
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • This does not consider string interning and caching of small integers. – lexicore Apr 29 '18 at 12:15
  • @lexicore I did not look into the implemention of this tool. It might omit the cache or interning and not reach the second level in your answer. – xingbin Apr 29 '18 at 12:24