Monday, 12 August 2013

When does object go out of scope if no variable is assigned?

When does object go out of scope if no variable is assigned?

When does the object of type list, occupying memory, become eligible for
garbage collection, Also where is the variable that holds reference to the
list ? In the case of code below, there was no variable assigned to it.
CASE 1:
for (Integer i : returnList()) {
System.out.println(i);
}
In case of a code like:
CASE 2:
List list = returnList();
for (Integer i : list) {
System.out.println(i);
}
list = null;
We can take control of GC, Is there any ways to take care of that in the
first case when no variable was assigned ?
To summarize:
What is the mechanism of referrence, without a reference variable to list
is case 1?
Does list get GC'd when stack frame is popped ?
Any way to speed up GC in case 1.

No comments:

Post a Comment