Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

garbage collection

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given:
12.void start(){
13.A a = new a();
14.B b = new b();
15.a.s(b);
16.b=null;
17.a=null;
18.System.out.println("start is over");
}
when the object B ,created in the line 14,eligible for garbage collection?
A.after the line 16.
B.after the line 17.
C.after the line 18
D.none of above
answer:C
who can tell me why?
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object is eligible for GC when is not reachable by any existing thread.
If I am not mistaken, the method a.s(b) receives as a parameter b, but we don't know what's gonna happen with that reference inside that method. It can be assigned to another reference, etc. So even if we execute line 16, we don't know for sure what really happened inside method s(). It is only eligible when the method is finished, which is out of scope.
Please correct me if I'm wrong
[ July 20, 2003: Message edited by: Andres Gonzalez ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder if there's missing code in this question?
I think Andres you're correct.
We don't know for certain what will happen to the b reference once s() method is called.
It might be reassigned to another variable of B type?
Alex
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the functioning of the a.s(b) is not provided then answer c can not be considered correct. I guess its a wrong question or you can a correct question with wrong answers.
[ July 20, 2003: Message edited by: Anupam Sinha ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kef,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct me if i am wrong....
No matter what happens in the method a.s(b), once b=null doesn't it mean all the variables pointing to this reference becomes null and so shouldn't the correct answer be 'after line 16'???
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
no matter what happens to b inside the method s(). but as long as object a is pointing to objcet b thru s(), only after a is set to null b is free for GC.
If Wrong...plz correct me!
JC
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
Well the reason for my earlier answer that it is not possible to know for sure whether b is elligible for GC or not is that the method a.s() may store the reference of b in some varibale. Consider this code for a.s()

Now in here, the object reference contained in o(b in the orignal code) is being transfered to a method in AnotherClass. This will prevent the varibale b from being GCed. Remember object references are GCed and not variables.
[ July 22, 2003: Message edited by: Anupam Sinha ]
 
Goodbye moon men. Hello tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic