相关疑难解决方法(0)

Java HashMap.clear()和remove()内存是否有效?

考虑以下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内存是否有效?

java memory hashmap

42
推荐指数
2
解决办法
9万
查看次数

标签 统计

hashmap ×1

java ×1

memory ×1