Tuesday, August 6, 2013

Calculating the size of Java Collections

This post is a simple case study demonstrating how the standard Java Collections are consuming memory. If you bear with me, I will guide you through the art of predicting and measuring the exact size of an ArrayList, HashMap or any other data structure found in the Collections library.

In this article we are looking for answers to a simple question. Namely – how much memory do my list and map consume after I have ran the following code. And how can you put this knowledge to practice estimating the collection size in your own code.
List list = new ArrayList();
Map map = new HashMap();
for (int i = 0; i < 1000; i++) {
    list.add(new Integer(i));
    map.put(new Integer(i), new Object());

Read more here 

Leave a Reply

All Tech News IN © 2011 DheTemplate.com & Main Blogger .