考虑以下HashMap.clear()代码:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
看来,对象的内部数组(table)Entry永远不会收缩.因此,当我向地图添加10000个元素时,在该调用之后map.clear(),它将在其内部数组中保留10000个空值.所以,我的问题是,JVM如何处理这个数组,因此,HashMap内存是否有效?