I have project that is handling a large amount of data that is being written to an excel file. I store this data in a static HashMap in the form Map<List<String>, Integer>, where the size of the list is only ever 3. The number of entries in the Map however can range anywhere from 0 to 11,300.
The flow of this project is:
Load Map up with entries
Iterate Map and do stuff
- Clear map for next set of entries
What I recently found out about HashMap though is how it re-sizes when the set size is breached. So not only is my Map re-sizing constantly at dramatic lengths, but it could very well have about 20,000 empty entries by the time I clear the largest set of entries.
So I'm trying to micro-optimize this thing and I'm stuck with a dilemma of how to do this. My two thoughts are to:
Set the default value of the initial HashMap to a value that would allow it to at most ever re-size only once
Reinitialize the HashMap with the average size that is expected for each new entry set to limit re-sizing and allow the garbage collector to do some clean up
My intuition tells me option two might be the most reasonable one, but that could still prove for lots of re-sizing depending the next entry set. But then option one greatly limits re-sizing to a one time operation but then leaves me with literally thousands of null entries.
Are one of my two proposed solutions better than the other, is there not much difference in memory improvement between the two, or could there be some other solution I have overseen (that does not involve changing the data structure)?
EDIT: Just for some context, I'm wanting to do this because occasionally the project runs out of heap memory and I'm trying to determine how much of an impact this gigantic map is or could be.
EDIT2: Just to clarify, the size of the Map itself is the larger value. The key size (i.e. the list) is ONLY ever at 3.
Listobjects as keys to map, then you have enough memory for the entire process. There is nothing real gained by shrinking the map, but you lose performance by the re-expansion. The memory saved by the shrinking is minimal, compare to everything else going on. This is or course assuming it's a continual process. Don't keep the large-but-empty map around for extended periods of time without using it. In that case, remove the map and reallocate it needed next.List, which is notComparable, preventing use ofTreeMap. Could supply a customComparator, but then you'd still have to decide on an ordering of the lists, which may not be feasible. Besides, aTreeMapuses more memory than aHashMapat it's peak.