当对象数组不再被引用时,该数组中的对象是否也会被垃圾回收? (假设没有变量引用元素)
在此页面中,http://java.sys-con.com/node/37613 它说 - “最大的危险是将对象放入集合中却忘记将其删除。该对象使用的内存将永远不会被回收。”
如果您确保使引用无效,为什么该内存将无人认领?
谢谢
最佳答案
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)
是的。
"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."
此时您持有对集合的引用。例如,如果您有一个 Map
,您在其中放置了一个键值,然后忘记删除它,那么它将永远保留在那里。想想 http session ,如果你在 ServerContext
中使用某些东西,或者在请求开始时使用 session id 作为键,但未能在请求处理结束时将其删除。
关于java - 在 Java 中,对象数组是如何被垃圾回收的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9627836/