This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes gc question - null is not eligible ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "gc question - null is not eligible ?" Watch "gc question - null is not eligible ?" New topic
Author

gc question - null is not eligible ?

donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
What, why, huh?

"Which is the earliest statement, where the object originally held in e, may be garbage collected:"


1. public class Test {
2. public static void main (String args []) {
3. Employee e = new Employee("Bob", 48);
4. e.calculatePay();
5. System.out.println(e.printDetails());
6. e = null;
7. e = new Employee("Denise", 36);
8. e.calculatePay();
9. System.out.println(e.printDetails());
10. }
11. }

the correct answer is line 7 and not line 6? Why not "the end of line 6"?

this is a dub question, right? not in intermediate question, right?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Notice that the answer doesn't say after line 7 executes. It just says once execution gets to line 7, the original object is eligible for garbage collection.
donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
The code is at line 7 when the "old" object held by "e" is up for a possible gc. Losely speaking. Not line 6, because that is the code that makes it
possible. Line 6 has to complete, at that point we're at line 7.
yikes.

Thank you!

Pavan Kumar
Greenhorn

Joined: Jun 08, 2005
Posts: 23
While you are at this topic.. Let me ask about garbage collection during function call return. How does the G.C handle all the Objects created in a function once the control goes back to the calling function??

public void foo(){
Line 1: String p = "Test";
Line 2: List lst = foo2(p);
Line 3: System.out.println();
}

private ArrayList foo2(String p){
ArrayList list;
-- Initalize arraylist and add 20000 objects ..
return list;
}

So in the above program when the control comes to Line 3, does G.C prepare the objects from foo2(String ) for Garbage Collection or has it until the control gets out of function foo()??

Thanks,
Pavan.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
An object is eligible for garbage collection if there are no references to it. The actual rules are a bit more complex, but that is what it seems to boil down to. If the value of the ArrayList object were not passed, it would be eligible for CG once foo terminates, since list is local to foo2.

list is merely a reference to an object, once foo2 ends, list doesn't exist but the object it used to point to still does and is not eligible for gc since lst references it.

When foo terminates, the ArrayList object created in foo2 and the String created in foo are now eligible for gc.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Pavan Kumar
Greenhorn

Joined: Jun 08, 2005
Posts: 23
Thanks David.. that really makes sense now about why my code was complaining abt running out of heap memory
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: gc question - null is not eligible ?
 
Similar Threads
John Hunt's Mock Exam Question
Garbage Collector?
Garbage Collector?
Q.31 from John Hunt
Garbage collection Q