• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GC

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
This question is from sum mock exam

Which statement is true?
A The NewGarb class will not compile.
B The getIt() method must not be declared as static.
C The NewGarb class compiles, but an exception is received because dg is not set to null.
D The rg object is eligible for garbage collection after a call to the getIt()
method has returned.

//I correctly marked the answered as d.
But what confuses me is...since the rg is returned it will have a link to the object after the call to getIt().
can some one help me how the rg is Garbage collected
-thanks in advance..
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony
Keep in mind that rg is a reference to an Object. When rg is set to equal null the object that it refered to no longer has any references to it, so it is eligible for garbage collection right after line 7. What is returned by the method is null, a reference to nothing.
hope that helps
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave,
By setting the reference to null can any object be made eligible for GC. Then is it possible if we set it to some value will it be still be GC after the method call.
Please advice
Thanks in advance
kareem
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kareem
Just setting a reference to an Object to null doesn't mena it will be garbage collected. There have to be no references left to an Object for it to be eligble for garbage collection. So if there are multiple references to a Object just setting one of them to null will not do the trick.
The value of a variable has no bearing on an object:
MyClass a = new MyClass();
MyClass b = new MyClass();
both a and b are variables that refer to Objects of type MyClass...
a = null;
The variable a is set to null - there is now no reference to the object that a refered to - so it is available to be gc'd...
a = new MyClass();
the variable a now referes to another object of type MyClass but the original one that it refered to is still eligible to be gc'd.
hope that helps
 
reply
    Bookmark Topic Watch Topic
  • New Topic