在Java中,如何对象垃圾的收集阵列? [英] In Java, how is an Array of objects garbage collected?
问题描述
当对象的数组不再被引用,这是否数组中的对象是垃圾回收吗? (假设没有变量引用的元素)
在此页面, http://java.sys-con.com/node/37613
它说 -
最大的危险是将对象放入集合,忘记将其取下。该对象使用的内存不会被回收。
如果你一定要废了引用,为什么会内存是无人认领的?
感谢
当对象的数组不再被引用,确实对象
该数组中的垃圾回收吗? (假设没有变量是
引用元素)
块引用>是
最大的危险是将对象放入集合,
忘了将其删除。该对象使用的内存永远不会
回收。
块引用>这是当你的是的抱着集合的引用。例如,如果你有在你把一个键值,然后忘记删除
地图那么它在那里停留,直到永远。想想HTTP会话,如果您使用的东西,在
ServerContext中
或一些这样在使用会话ID作为密钥请求开始,但未能在请求处理结束时将其删除..When an array of objects is not referenced anymore, does the objects in that array are garbage collected too? (assuming no variables are referencing the elements)
In this page, http://java.sys-con.com/node/37613 it says - "The biggest danger is placing an object into a collection and forgetting to remove it. The memory used by that object will never be reclaimed."
If you make sure to nullify the references, why will that memory be unclaimed?
Thanks
解决方案When an array of objects is not referenced anymore, does the objects in that array are garbage collected too? (assuming no variables are referencing the elements)
Yes.
"The biggest danger is placing an object into a collection and forgetting to remove it. The memory used by that object will never be reclaimed."
This is when you are holding a reference to the collection. For example, if you have a
Map
in which you put a key-value and then forget to remove then it stays there for ever. Think http sessions, if you use something inServerContext
or some such at start of request using session id as key but fail to remove it at end of the request processing..这篇关于在Java中,如何对象垃圾的收集阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!