Java GarbageCollection和集合

时间:2016-04-04 16:06:21

标签: java collections garbage-collection

说我做这样的事情:

List<Stuff> methodA() {
    List<Stuff> all_the_stuff_to_do = new ArrayList<>();
    all_the_stuff_to_do.add(new Stuff("important"));
    all_the_stuff_to_do.add(new Stuff("not important"));
    return all_the_stuff_to_do;
}

List<Stuff> methodB() {
     List<Stuff> important_stuff_to_do = new ArrayList<>();
     Stuff important = methodA().get(0);

     // at this point Stuff("not important") is not reachable anymore
     // since I have no reference to the list that contains it left

     important_stuff_to_do.add(important);  
     return important_stuff_to_do;
}

void methodC() {
     ... do a happydance ...

     List<Stuff> stuff_to_do = methodB();

     ... look sad because now I got important stuff to do ...
}

*****编辑*****
更好的澄清和简化的代码

澄清:

当退出 methodA()时,我得到了一个列表的引用,其中包含两个对象 Stuff(“important”) Stuff(“not important”)< /强>

我将对Stuff(“重要”)的引用添加到 methodB()中的列表中。 此时,无法再访问对象 Stuff(“不重要”)。 此外, methodA()中的列表不再可用。

但List仍然包含对确实可到达的Object的引用,即 Stuff(“important”)

何时清除 all_the_stuff_to_do 列表以及资料(“不重要”)对象?

是否会在致电 MethodA 后直接进行?或者它会在 MethodB 的末尾?或者从来没有因为它包含对 Stuff(“important”)对象的引用,该对象在程序结束前仍处于活动状态?

3 个答案:

答案 0 :(得分:1)

该集合包含对您添加的对象的引用。在此之前无法访问或从集合中删除对象之后,它们将可以访问。如果不是这样的话,你能想到程序能否可靠运行的方式吗?

答案 1 :(得分:1)

Will (all_the_stuff_to_do be garbage collected) directly after the Call to MethodA? Or will it be at the end of MethodB? Or never since it contains a reference to the Stuff("important") Object that is still active till the end of the program?

Garbage collection is generally done on a low priority thread. If nothing else is being done, the garbage collector may run.

If the VM is low on, or out of, memory, the garbage collector may run immediately as the highest priority thread, since it needs to recover memory for the program's immediate need.

So, will it be collected directly after the call to MethodA? No. Will it get collected after the call to get(0)? Possible, but unlikely. At the end of MethodB? Possible, but unlikely. Still active at the end of the program? Possible, but only likely if the program never ran out of memory and never became idle.

The fact that the list contains a copy of "Stuff("important") Object" is irrelevant to whether the list gets collected. If no accessible objects reference the list, the list is eligible for garbage collection.

Will the list get #clear()'d during collection? No, there is no need. If it was a user defined collection, the clear method could do anything, including add another reference to the list, which would mess up the garbage collection. Instead, it is just collected, and objects the list refer to are referenced one less time. That include "Stuff("important") Object" ... It's reference count will get decremented, but since a reference still exists, it won't be cleared.

答案 2 :(得分:0)

In your scenario new Stuff("important") here new keyword is responsible to making an object and reference of this Stuff Object hold by the collection List <Stuff> important_stuff_to_do. Now this collection will hold the reference of the two object that you made .

As per Collection definition all we know that Collection is a group of multiple Object of same type of Objects as a single Entity.

So, No Garbage Collection will be perform because here these two objects are still reachable.

new Stuff("important")
new Stuff("not important")

Note:- Garbage Collection performs only objects which is completely unreachable (One kind of Orphan). Here No Object is getting orphan because Collection never makes the copy / cloing of the objects which is added .

Conclusion :- No Garbage will be performed in this scenario. Thank you