Published in


Some thoughts on using LinkedList in Java

This is going to be a different article as my assumption after testing some on my machine turned out to be slightly wrong. Here's what I found.

Use ArrayList in most standard cases where you simply add to the list and later on iterate over it. It will save you both time and memory.

Note that this article does not discuss the complexities of some of their operations where we know one or the other would perform substantially better (e.g. adding elements in the beginning of the list or random access). The use case discussed here is simply appending items in order to iterate over it at some later point.

My hypothesis was that LinkedList would perform better compared to ArrayList when simply adding elements to the end of the list in all cases. And I would then recommend that we should turn away from by default typing

and instead, write


Because ArrayList has to perform resizing operations once the inner container fills up while the LinkedList simply has to append a new node (the same operation regardless the size).

The test

So I set up a little test of adding 10M elements to each type of list, see below.

This is a dummy example but similar setups are not unlikely to be seen in production. My results were that they performed very similarly.

The test was run with the following VM options to remove any issues with adaptive heap sizing or GC.

First test result with epsilon GC.

Now obviously this is pretty far from reality so I decided to modify the test with just specifying the G1 garbage collector.

See below for the new test.

The difference here is that I’ve averaged the times which introduces some extra warmup so the compiler has a bit more time to optimize and we achieve a more realistic result.

More realistic results.

What happened?

It’s difficult to say if the difference between ArrayList and LinkedList in the final example is due to compiler optimizations being more effective for the ArrayList, effects of GC or as a result of the LinkedList requiring more memory. To figure that out one would have to dig into the compiler & GC logs.

But for now, we can conclude that due to either of those effects ArrayList seems to perform substantially better than LinkedList using a more realistic setup with an actual GC running.

Finally some memory considerations

I performed the following test and noted the memory differences.

Memory test for 100 elements.

Following table illustrates the results in a retained heap when doing the aforementioned test with 100 and 1000 Integer elements (which takes 16B since int takes 4 bytes and header info takes 12 [1]) each as reported by MAT [2].

Overhead here is simply the retained heap per item minus 16B as provided by MAT less some header info of the actual list. As expected the LinkedList takes up roughly double memory compared to the ArrayList. However, this is given a small element, with bigger elements the overhead will be negligible.


My takeaway from this is to prefer ArrayList over LinkedList in most standard cases where the use case is to simply add to the end and iterate over the list.


[1] How to calculate the memory usage of Java objects

[2] Memory Analyzer (MAT)



A humble place to learn Java and Programming better.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store