*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection on method arguments (from Enthuware) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection on method arguments (from Enthuware)" Watch "Garbage Collection on method arguments (from Enthuware)" New topic
Author

Garbage Collection on method arguments (from Enthuware)

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
This is from Enthuware (Test 3, Question 30)


After which line will the object passed to the method getObject() be eligible for garbage collection?

public void getObject(Object a)
{
Object c, b = new Object(); //1
//do something with a
c = b; //2
a = c;//3
b = a = null; //4
c = null;
}

The answer provided is never, citing the fact that the object pointed to by the method parameter a must have been created outside and thus there must be an external reference to it.

But I think that if you call the method this way:

getObject(new Object()); // No intervening reference

The object will be eligible for GC at line //3.

Does this sound reasonable?

Thanks.


All code in my posts, unless a source is explicitly mentioned, is my own.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I still think that would be considered an outside reference and would not be eligable for garbage collection until after the method completed. Wouldn't the compiler create the new Object with no named reference and then pass it into the method, wait for the method to complete, then you would lose the reference to it?

SCJA
~Currently preparing for SCJP6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Brian,

I just ran a test and it seems that the argument method can indeed be garbage collected before the method returns:

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3179
    
  49

Nice demonstration Ruben.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Thank you Devaka. Maybe I learned something from your great ExamLab examples.
 
jQuery in Action, 2nd edition
 
subject: Garbage Collection on method arguments (from Enthuware)