my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About GC 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About GC" Watch "About GC" New topic
Author

About GC

Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
When is the Object o, eligible for gc ?

I guess the answer should be "after the test() method completes" (because the method test() does not return the reference to outside), am I right?

Thanks in advance!
Jack
Panos Liaskos
Ranch Hand

Joined: Jul 28, 2003
Posts: 34
Jack,
I don't think that the object referenced by the o reference variable is eligible for gc.
The only object that is eligible for gc is the one referenced by the obj reference variable (line: obj = new Object() .
After the method completes the local method reference variable obj is out of scope (so it doesn't exist outside the scope) but there is still in memory the object that it refers. That object is eligible for GC.
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Then when would Object o eligible for gc ?
If I add the following code, will Object o eligible for gc ?

Thanks,
Jack
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Yes both t and o will be eligible for g.c.
You can write a program that confirms it.

[ October 26, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Panos Liaskos
Ranch Hand

Joined: Jul 28, 2003
Posts: 34
Lets see the hole code:

Thats what I think is happening:
1. after takeObject method completes: 1 object eligible for GC (the one referenced by obj local reference variable).
2. after test method completes: another 1 object eligible for GC (referenced by o local reference variable)
3. so after t.test() returns: 2 eligible objects.
4.next line is: t=null; Another object (of type Test) eligible for GC (referenced by t reference variable)
5. so when this line completes you have: 3 objects eligible for GC.
Panos Liaskos
Ranch Hand

Joined: Jul 28, 2003
Posts: 34
For the exam just note on Jose's excellent (and enlighting) example that System.gc(); has no guaranteed behavior.
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Thank you very much!!
Jack
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About GC
 
Similar Threads
Brogden's Garbage Collection?