Java ArrayList.clear() 方法是否释放内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18528055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does ArrayList.clear() method frees memory?
提问by nstosic
If I'm not mistaken, ArrayList contains the value of memory locations in which the variables you've added to the List are stored. So, my assumption is that when you call ArrayList.clear() method it only frees the aforementioned values (of memory locations) but doesn't free those memory locations themselves. I'll try to illustrate this with an example:
如果我没记错的话,ArrayList 包含存储您添加到 List 的变量的内存位置的值。因此,我的假设是,当您调用 ArrayList.clear() 方法时,它只会释放上述值(内存位置),但不会释放这些内存位置本身。我将尝试用一个例子来说明这一点:
Let's say you've got current status of memory:
假设您有当前的内存状态:
[Memory location] (type of variable) *value*
[1000] (int) 32
[1002] (int) 12
[1003] (float) 2.5
And you add them to the list myList, so it contains pointers to the 1000, 1002, 1003 memory locations.
然后将它们添加到列表 myList,因此它包含指向 1000、1002、1003 个内存位置的指针。
When you call myList.clear() the pointers will be nullified, but the memory locations 1000, 1002, 1003 would still contain previously given values. Am I wrong?
当您调用 myList.clear() 时,指针将被取消,但内存位置 1000、1002、1003 仍将包含先前给定的值。我错了吗?
采纳答案by Karthik T
You are correct because the memory is cleared asynchronously by the Garbage collector, and not directly by such library functions. clear
would only null
them all out, and update the ArrayList
state accordingly.
您是对的,因为内存是由垃圾收集器异步清除的,而不是由此类库函数直接清除。clear
只会将null
它们全部清除,并相应地更新ArrayList
状态。
If any or all of those elements are not not referenced by any other portion of your code, they become eligible for garbage collection, and might be destroyed + de-allocated any time from shortly after that call to the end of time.
如果这些元素中的任何一个或所有元素没有被您的代码的任何其他部分引用,则它们有资格进行垃圾收集,并且可能会在调用后不久到时间结束的任何时间被销毁 + 取消分配。
回答by Evgeniy Dorofeev
clear
only nulls backing array elements
clear
只有空值支持数组元素
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
but if we call ArrayList.trimToSize
after clear
backing array will shrink
但是如果我们调用ArrayList.trimToSize
after clear
后备数组会缩小
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
回答by Suresh Atta
No,It wont clear the memory.After you clear()
.
不,它不会清除内存clear()
。在你之后。
See the source code of clear()
查看clear()的源码
public void More ...clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
The method is making the elements eligible for Garbage Collector.Not clearing them immediately. Once GC run's,They are gone.
该方法使元素符合垃圾收集器的条件。不立即清除它们。一旦 GC 运行,它们就消失了。
回答by Amol Sonawane
The answer to your question, it will not FREE
the memory. It will just remove all the object references
. Now these objects may or may not be collected by garbage
collector depending on how they are referenced at other places.
你的问题的答案,它不会FREE
记忆。它只会删除所有object references
. 现在这些对象可能会或可能不会被收集garbage
器收集,这取决于它们在其他地方的引用方式。
For instance,
例如,
Object 1 refers - > Object 2
ArrayList refers - > Object 2
ArrayList refers - > Object 3
In this case, after Arraylist.clear()
, object 3 will be eligible for clean-up
but not object 2.
在这种情况下,在 之后Arraylist.clear()
,对象 3 将符合条件,clean-up
但不符合对象 2 的条件。