0

Which one is better from memory point of view String[] / List<String>. I have 5000 or more objects need to store which i am getting as a response from service.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
frq
  • 29
  • 2
  • 7
  • 1
    possible duplicate of [Array or List in Java. Which is faster?](http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster) – Andy Jan 16 '15 at 10:23
  • I dont think this problem is associated with memory-leak issues. – Szarpul Jan 16 '15 at 10:24

4 Answers4

2

If depends on the List implementation you are asking about. If you are asking about String[] vs. ArrayList, they use the same amount of storage, as the ArrayList is backed by an array (the difference will depend on the length you initialize the array with vs. the initial capacity you start the ArrayList with).

In order to use String[], you must know the max number of Strings you'll need to store, since the array's legnth is fixed. The ArrayList will automatically copy the data to a larger array whenever necessary.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

The simpler object is always better in memory. It is highly dependend on what list you are using. LinkedList is way worse in memory than ArrayList. The question you should ask yourself is how you will interact with the set. If you want to add sth. in the middle you should use a LinkedList. If just have a fixed set of Strings use the String[]. If you are just adding more and more Strings use the ArrayList. You shouldn't really worry about memory usage, since there is plenty.

xeed
  • 925
  • 8
  • 22
1

Probably you should go for ArrayList<String>.

The memory consumption depends on the type of List, and frankly, for only 5000 objects that still hardly matters.

ArrayList<String> is only slightly "worse" than String[]. ArrayList<String> simply wraps and manages an Object[], giving overhead per object and per growth threshold. How big that Object[] is doesn't matter. And each entry in an Object[] would usually be 4 bytes in size, no matter whether you use it or not, as Java is by reference. So that would be like 20 kiB. That hardly matters in most of the environments in which Java is used.

If you use LinkedList<String>, the memory consumption will be more, as for every entry, there will be an additional Node object. But still that hardly matters. Let's assume each Node object is 20 Bytes in size (hashCode, class, list, next, previous). Then we're talking of 100 kiB.

In the context of 1 GiB RAM on a phone it already doesn't matter for most applications on a phone. On a PC with 8 GiB or 16 GiB, you really don't want to mind.

I'd usually go for ArrayList<?>, except if I remove and add the current loop element frequently and do not need random access, then I go for LinkedList<?>, or for CopyOnWriteArrayList<?> if I need a list that I can read and modify from multiple threads at the same time and the list wouldn't grow too big or write is rare.

Christian Hujer
  • 17,035
  • 5
  • 40
  • 47
0

String[] and List<String> both taking same space in memory to store data.

String[] - you need to have all information how much elements get stored in your memory, and it should be not minimum or not maximum space then required space.

List<String> - It has its own benefits of resizing, specify certain size to your list in case specified size is get small in feature you no need to change the code. and List collection is easier for your further operations.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50