Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

gc question - null is not eligible ?

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pavan Kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David.. that really makes sense now about why my code was complaining abt running out of heap memory
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic