Xtroce
Xtroce

Reputation: 1824

Java GarbageCollection and Collections

Say I do something like this:

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

***** EDIT *****
Better clarification and simplified code

To Clarify:

When exiting methodA() I got a reference of a list with two objects Stuff("important") and Stuff("not important")

I add the reference to Stuff("important") to a list in methodB(). At this point the Object Stuff("not important") is not reachable anymore. Also the List in methodA() is not reachable anymore.

But the List still contains a reference to an Object that is indeed reachable, namely Stuff("important").

When will the all_the_stuff_to_do List be cleared and when the Stuff("not important") Object?

Will it be 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?

Upvotes: 0

Views: 82

Answers (3)

AJNeufeld
AJNeufeld

Reputation: 8695

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.

Upvotes: 1

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

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

Upvotes: 0

JimmyJames
JimmyJames

Reputation: 1403

The collection contains references to the objects that you have added. Until that is no longer reachable or you remove the objects from the collection, they will be reachable. Can you think of a way programs could work reliably if it were otherwise?

Upvotes: 1

Related Questions