Is java HashMap.clear() and remove() memory effective?
Posted
by Shaman
on Stack Overflow
See other posts from Stack Overflow
or by Shaman
Published on 2010-05-11T14:29:56Z
Indexed on
2010/05/11
14:34 UTC
Read the original article
Hit count: 375
Consider the follwing HashMap.clear() code:
/**
* Removes all of the mappings from this map.
* The map will be empty after this call returns.
*/
public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
It seems, that the internal array (table) of Entrys is never shrinked. So, when I add 10000 elements to a map, and after that call map.clear(), it will keep 10000 nulls in it's internal array. So, my question is, how does JVM handle this array of nothing, and thus, is HashMap memory effective?
© Stack Overflow or respective owner