Win a copy of Head First Android this week in the Android 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

garbage collector

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Which of the following statements about garbage collection are true??
(Select two correct answers)
A: Only objects with no references will be garbage
collected.
B: objects can be immediately garbage collected by
setting them to null.
C: Setting an object to null will make it eligible
for garbage collection.
D: Garbage collection cannot be forced.
The answere is C and D. But I think that is A and D.
if A is not correct, When objects with no references will not be garbage collected?.
And if C is correct, why the object will be garbage collected if have another reference???
Object o1 = new Object();
Object o2 = o1;
o1 = null;
Thank you in advance.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Jordi, in these kind of questions you are to choose 2 most precise answers. In this case the option C and D are the most precise and accurate.

------------------
MCP 2000, CCNA , SCJP 2
http://java.ditmas.net
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jordi,
Exercise care in reading the answers as well as the ques.
A: Not correct(see key word 'Only')
This is because objects with no references AND objects set to null will be eligible for gc.
C: & D: Are correct
Quote from Vel's notes on Chapter 1 - Lang Fund: "An object can become eligible for gc even if there are references pointing to the object, as long as the objects with the references are also eligble for gc. If the object is still in use, it will not be gc leaving the reference dangling".

Object o1 = new Object();
Object o2 = o1;
o1 = null; // hints at being eligible for gc(nothing is guaranteed)
Hope that helps

------------------
Percy Densmore
-SCJP2 Die Hard Student
[This message has been edited by Percy Densmore (edited September 10, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Percy,
I Think that in the code below are two references and one object
Object o1 = new Object();
Object o2 = o1;
o1 = null;
for this I believe that the object will not be garbage collected because still have another reference.
Why I am wrong?
Sorry for my bad english.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jordi,
The reason why A is not correct is bcoz,
it is not necessary that if an object is not being referenced then only it will be garbage collected.There are times when the reference of the object is still being held by some reference variable and yet it becomes eligible for GC.This is the case when the reference becomes dead or is no more active.Here u need not put an explicit null into make it eligible for GC.The GC will itself come to find whether it is active or has become dead.An example will clear this:

class Aclass {
protected void finalize()throws Throwable {
System.out.println("Aclass.finalize()");
}
}
class Bclass {
Aclass a1=new Aclass();//reference as a member of class
protected void finalize()throws Throwable{
System.out.println("Bclass.finalize()");
}
}
public class ABclassDemo {
public static void main(String[]arg ){
Bclass b1=new Bclass();
b1=null;

//as soon as you put null Aclass reference in Bclass becomes dead
//hence eligible for GC
//so it is not necessary objects with no reference will alone be eligible for GC
//objects with reference could also be GC'd if reference becomes dead

System.gc();
}
}

Now if u consider C statement,in its own context it is right,but only when all the references to that object is either set to null or becomes dead.
THANKS
reply
    Bookmark Topic Watch Topic
  • New Topic