• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Garbage collection

 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10110
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, 2 objects are eligible for gc.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does f() send or keep a copy of the reference somewhere?
[ November 21, 2006: Message edited by: Bert Bates ]
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10110
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred,

You are right. The info given from the wuestion is not enough.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic