In this article we are looking for answers to a simple question. Namely – how much memory do an instance of a list or map consume after I have created a simple testcase. And how can you put this knowledge to practice estimating the collection size in your own code.
The list is a simple ArrayList containing 1,000 Integers and our map is created as HashMap with 1,000 Integers as keys and 1,000 freshly created new Object() instances as values. Now for those who are just after an easy answer - measuring the sizes using Aleksey Shipilev’s utility gave us the following answers on my 64bit Mac OS X:
- The size of the list is 20,976 bytes
- The size of the map is 68,168 bytes
For those interested in how those numbers were calculated, continue reading the full blog post.