在Java中,如何对垃圾收集对象数组?

时间:2022-09-07 23:58:06

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."

在这个页面中,http://java.sys-con.com/node/376​​13它说 - “最大的危险是将一个对象放入一个集合而忘记删除它。该对象使用的内存永远不会被回收。 “

If you make sure to nullify the references, why will that memory be unclaimed?

如果确保使引用无效,为什么要无人认领该内存?

Thanks

4 个解决方案

#1


4  

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..

这是当您持有对该集合的引用时。例如,如果你有一个Map,你在其中放置一个键值,然后忘记删除它然后永远保持在那里。想想http会话,如果你在ServerContext中使用某些东西,或者在使用session id作为密钥的请求开始时使用某些东西,但是在请求处理结束时无法删除它。

#2


2  

For the first question, the answer is yes, absolutely: the objects inside non-referenced array and no other references do get garbage collected.

对于第一个问题,答案是肯定的,绝对:非引用数组中的对象和其他引用都没有收集垃圾。

As for the second question, the document talks about placing forgetting an object inside a referenced collection, for example a cache of some sort, a static field, a thread-local store, etc.

关于第二个问题,该文档讨论了如何将遗忘对象置于引用的集合中,例如某种缓存,静态字段,线程本地存储等。

#3


1  

It won't be unclaimed if nothing references it. The article says that if you nullify a reference, but the object is still in a referenced collection (hence referenced), it won't be collected.

如果没有引用它,它将不会被无人认领。文章说如果你取消引用,但是对象仍然在引用的集合中(因此引用),它将不会被收集。

#4


1  

Generally speaking, anything that's not referenced is garbage collected. So, yes, those objects would be garbage collected.

一般来说,任何未引用的内容都是垃圾回收。所以,是的,那些对象将被垃圾收集。

Also, note that:

另请注意:

  1. An array is not a collection.
  2. 数组不是集合。

  3. I think that what the person that wrote that meant was make sure you remember all of the places you're referencing an object, so that if you intend to remove it, it gets removed (and there are no lingering references to it).
  4. 我认为写这个人的意思是确保你记住你引用一个对象的所有地方,所以如果你打算删除它,它就会被删除(并且没有延迟引用它)。

#1


4  

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..

这是当您持有对该集合的引用时。例如,如果你有一个Map,你在其中放置一个键值,然后忘记删除它然后永远保持在那里。想想http会话,如果你在ServerContext中使用某些东西,或者在使用session id作为密钥的请求开始时使用某些东西,但是在请求处理结束时无法删除它。

#2


2  

For the first question, the answer is yes, absolutely: the objects inside non-referenced array and no other references do get garbage collected.

对于第一个问题,答案是肯定的,绝对:非引用数组中的对象和其他引用都没有收集垃圾。

As for the second question, the document talks about placing forgetting an object inside a referenced collection, for example a cache of some sort, a static field, a thread-local store, etc.

关于第二个问题,该文档讨论了如何将遗忘对象置于引用的集合中,例如某种缓存,静态字段,线程本地存储等。

#3


1  

It won't be unclaimed if nothing references it. The article says that if you nullify a reference, but the object is still in a referenced collection (hence referenced), it won't be collected.

如果没有引用它,它将不会被无人认领。文章说如果你取消引用,但是对象仍然在引用的集合中(因此引用),它将不会被收集。

#4


1  

Generally speaking, anything that's not referenced is garbage collected. So, yes, those objects would be garbage collected.

一般来说,任何未引用的内容都是垃圾回收。所以,是的,那些对象将被垃圾收集。

Also, note that:

另请注意:

  1. An array is not a collection.
  2. 数组不是集合。

  3. I think that what the person that wrote that meant was make sure you remember all of the places you're referencing an object, so that if you intend to remove it, it gets removed (and there are no lingering references to it).
  4. 我认为写这个人的意思是确保你记住你引用一个对象的所有地方,所以如果你打算删除它,它就会被删除(并且没有延迟引用它)。