I was inspired by this topic: Performance and Memory allocation comparision between List and Set to actually run some tests and measure the performance difference between ArrayList
and HashSet
.
The most upvoted answer, in the mentioned topic, that intrigued me a lot (link), says:
HashSet consumes about 5.5 times more memory than ArrayList for the same number of elements
With the help of ScalaMeter I wanted to make sure about this.
I made two, simple tests, adding from 10000
to 100000
elements to both ArrayList
and HashSet
. Setting the initial size to the maximum did not change the results. I tested those collections with two types:
Int
(putting consecutive numbers 0 to 100000)String
(putting random String using ApacheRandomStringUtils
)
The code is available on my repository here.
And running those, gave me this results:
- X-axis - size -> size of the collection
- Y-axis - value -> amount of kB used
For collections holding String
of size 10:
For collections holding String
of size 50:
The question:
What happened to the theory mentioned in the quoted answer? Is it false? Or probably there is some mistake on my side?
Thanks :)!
Update after @andrzej answer I have updated the code (and the repository) once again. The results are getting better but still the results are not 5.5x different. I am checking something more now.