在Java中,如何对象垃圾的收集阵列? [英] In Java, how is an Array of objects garbage collected?

查看:86
本文介绍了在Java中,如何对象垃圾的收集阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当对象的数组不再被引用,这是否数组中的对象是垃圾回收吗? (假设没有变量引用的元素)

在此页面, http://java.sys-con.com/node/376​​13
它说 -
最大的危险是将对象放入集合,忘记将其取下。该对象使用的内存不会被回收。

如果你一定要废了引用,为什么会内存是无人认领的?

感谢


解决方案

  

当对象的数组不再被引用,确实对象
  该数组中的垃圾回收吗? (假设没有变量是
  引用元素)



  

最大的危险是将对象放入集合,
  忘了将其删除。该对象使用的内存永远不会
  回收。


这是当你的的抱着集合的引用。例如,如果你有在你把一个键值​​,然后忘记删除地图那么它在那里停留,直到永远。想想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 in ServerContext 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆