In my class I create about 10 different Maps. I thought of initializing the size. But I also thought is there going to be an advantage in doing so.
Approach1:
Map<String,String> myMap = new HashMap<String, String>(5);
Approch2:
Map<String,String> myMap = new HashMap<String, String>();
Per Approach2, the default capacity is 16. But will the memory allocation is more for approach2 compare to approach1?


LinkBack URL
About LinkBacks
Reply With Quote
If you're interested, you can try it both ways and inspect the heap and memory usage with a tool like jvisualvm. Unless you're writing code that's at the core of a high-throughput application, you're unlikely to feel the benefit of such a hand optimisation. Additionally, you may not see the same difference in memory allocation from one JVM to another, or possibly - thanks to the unpredictability of the garbage collector - even from one run of your code to another.