ArrayList 调用clear()后内存地址空间释放问题

之前想清空集合的元素,直接调用api clear()函数,突然想如果clear后只是把元素删除了,而没有释放内存地址空间,因为ArrayList是动态的分配内存,以后越来越多,会不会导致内存溢出。查看clear()函数源码:

 /**
     * Removes all of the elements from this list.  The list will
     * be empty after this call returns.
     */
    public void clear() {
        modCount++;

        // clear to let GC do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }
可以看到,把集合中元素赋值为null,gcc会对其内存进行回收。

猜你喜欢

转载自blog.csdn.net/bobozai86/article/details/80117326
今日推荐