开发者

Cannot create a LinkedList of 7M Long type variables in Java

I'm trying to create a very large linked list as below, but it failed (ran as a unit test in maven. Already set heap size by running "set MAVEN_OPTS=-Xmx4096m" (i'm running in windows).

The code failed after inserting about 6M (6000000) Long items into the list. Why? Considering Long type is 8 bytes, 6M Long type variables are just 48M bytes. Even if Java object has some additional hidden fields, it shouldn't fail so early.

 int N = 100000000;

    try {

        LinkedList<Long> buffer = new LinkedList<Long> ();

        for(int i=0;i<N;开发者_如何学Go++i) {
            buffer.add((long)i);

            if (i % 1000000 == 0) {
                System.out.println("added " + i);

            }
       }
   catch(Exception e)
   {...}


Each entry in the list is a separate object, with a reference to the previous and next nodes, as well as the current value.

If we assume 8 bytes per reference, and an object overhead of 16 bytes, that means for each entry you've got:

  • A entry object: 40 bytes (3 references + overhead)
  • A Long object: 24 bytes (data + overhead)

So after 6000000 entries that would be about 384M... which you should still be okay with. (Depending on your JVM, I'd expect the reference size and per object overhead to be lower, too.)

I wonder whether your MAVEN_OPTS is either being set in the wrong place, or not used for JVM arguments for some reason. I've just tried running this on my Windows box (not as a unit test - just as a main method) and with the JVM default allocation, it fails after 6 million entries for me too. With -Xmx1024M it gets to 25 million entries, which suggests a smaller overhead than I've estimated above. (I'm on a 32-bit VM though.)

That certainly suggests your MAVEN_OPTS isn't doing what you want...


LinkedList is about the most memory inefficient way to store a series of longs. You have the Long object which is about 3x larger than a plain long and you have a linkedList entry which is doubly linked making its about 5x larger than a plain long.

I sugegst you use a long[] or a wrapper like TLongArrayList which use almost 8 bytes per long for a large collection. (It will be faster too)


Don't forget the memory taken up by

LinkedList$Entry
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜