0

In production I will have to populate pretty large collection of string (containing about 100M strings). I don't need to iterate through it, just populate and leave it as is.

I will have to persists it, but it's the responsibility of another thread.

I thought it was more efficient to populate LinkedList than ArrayList. Here is the simple program:

public static void main(String[] args) {
    long start2 = System.nanoTime();
    Collection<String> col = new ArrayList<>(); // <---HERE
    for (long i = 0; i < 100000000; i++) {
        col.add(String.valueOf(i));
        if (i % 1000000 == 0)
            System.out.println(i);
    }
    long end2 = System.nanoTime();
    System.out.println(end2 - start2);
    System.out.println(col.hashCode() == System.nanoTime());
}

For ArrayList the average result is: 61535592462 But if I replace it with LinkedList the program does not even finish. It always stops at 84000000.

Why that? Could you explain potential cause for these behavior? Is it platform-specific?

I use Ubuntu 16.04 + java 8.

UPD: After pretty long time, the program with LinkedList finshes with this:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.LinkedList.linkLast(LinkedList.java:142)
    at java.util.LinkedList.add(LinkedList.java:338)
    at com.test.App.main(App.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
user3663882
  • 6,957
  • 10
  • 51
  • 92
  • 1
    LinkedList has more overhead, so it's possible you hit a memory limit. – Eran Aug 09 '16 at 09:26
  • @Eran I'm just perplexed. Iterating through ArrayList is faster than through LinkedList. Also populating ArrayList is more efficient as well. What is the use of LinkedList than? – user3663882 Aug 09 '16 at 09:27
  • 1
    LinkedList vs. ArrayList: http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist – Manu Aug 09 '16 at 09:28
  • 2
    Anyway, ArrayList is more efficient for this code, as long as you call ensureCapacity(100000000) or instantiate it with a large capacity, to avoid multiple resizes. – Eran Aug 09 '16 at 09:29
  • 1
    @user3663882 You do realize that if you're going to persist them, and if you don't need to do any operations on them, it's a really stupid idea to populate them all first and waste memory instead of doing it in batches? – Kayaman Aug 09 '16 at 09:49
  • @Kayaman I cannot do it in batches. We use akka which makes snapshot of the entire currently observable state – user3663882 Aug 09 '16 at 09:51
  • Well that's a different story then. – Kayaman Aug 09 '16 at 09:52
  • @Kayaman Kind of, but I just didn't think it related to the actual problem I was trying to solve. The main issue in my case is performance with which I can populate the list and allow the actor to handle another message. – user3663882 Aug 09 '16 at 09:54

0 Answers0