aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Garbage collection" Watch "Another Garbage collection" New topic
Author

Another Garbage collection

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
Assume my class is a class that has 2 methods f() and g().How many objects will be eligible for garbage collection by the time you invoke g() ?
Myclass c1 =new Myclass();
Myclass c2 =new Myclass();
f(c1);
c1 = null;
c2 =c1;
g();


options

1) 0
2) 1
3) 2
4)The given information is insufficient.

My opinion is 2 objects are eligible for garbage collection.Am i right or wrong?please correct me.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

You are right, 2 objects are eligible for gc.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Does f() send or keep a copy of the reference somewhere?
[ November 21, 2006: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

i've always been a little shaky on GC, but i'd think 4 would be the right answer. mostly because you don't know what f() is doing with the reference. it is possible that object gets a new reference in that method, and would then therefore not be eligible.

I'm willing to listen to someone tell me why i'm wrong, though.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Fred,

You are right. The info given from the wuestion is not enough.
 
Consider Paul's rocket mass heater.
 
subject: Another Garbage collection