aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects eligible for garbage collection. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects eligible for garbage collection." Watch "Objects eligible for garbage collection." New topic
Author

Objects eligible for garbage collection.

Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Pls have a look at the following code:
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
The Answer given in the explanation of this code is 3. Shouldn't it be 4.

If 3 then please explain how.

Thanks & regards.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Question for you: what is reference variable a's value at that point in the program? And b's value? And c's value?
[ January 17, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Harish Paravasthu
Greenhorn

Joined: Dec 21, 2006
Posts: 23
can any one say what actually it means by d=c=b=a here in code???
i think all are having the same reference to one object...but if so what is that reference???


Harish Paravasthu
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
We are creating 4 objects. After the statement d=c=b=a, all the four references are pointing to the first object. Since the other 3 objects are now not referred by any other object references, they will be garbage collected.
Even after setting d to null, will not garbage collect the first object, since it is still referred by a, b and c.


SCJP 1.5<br />SCWCD 1.4
Harish Paravasthu
Greenhorn

Joined: Dec 21, 2006
Posts: 23
public class Test{
public static void main(String[] args)
{
Object a = new Object();
// the object original referenced by object reference a
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c;
c=b;
b=a;
d=null;
}
}

A small change to the above code...here in this case,how many objects go into garbage collected???

i think only one object...
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Hi Barry Gaunt ,




Statement d=c=b=a implies that all are pointing to the same object i.e. to the Object refered by reference variable a.

i.e. d,c,b and a all will be referening same object. So as per my knowledge, objects created at 2,3,4 will be abondened and are eligible for GC. Finally at step 5, since d is assigned null so the object will become eligible for GC.

so overall 4 objects will become eligible for GC.

Please correct me if I am wrong.

Thanks,
Ritu
Harish Paravasthu
Greenhorn

Joined: Dec 21, 2006
Posts: 23
Hai Ritu,

you told that all references will be having a copy of a reference,so by that time b,c and d are garbage collected,so though you dereference d to null,its already vanished away in the picture.so only three objects are garbage collected,

hope you got cleared...

Harish
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Sorry but could not understand. If possible could you pls elaborate.
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
Please think about the question by Barry. Surely you will get the answer.

Question for you: what is reference variable a's value at that point in the program? And b's value? And c's value?
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288


This is the same as this code:



So the objects created at 2,3 and 4 are no longer referenced by any variables and are eligible for GC. There are now three references to the same object created at step 1.
[ January 17, 2007: Message edited by: Mark Smyth ]

SCJP<br />SCJD
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Yeah now I got it.. Thanks a lot.

When d is assigned null, even then a,b,and c are refering to the same object. so only 3 objects are eligible for gc.

Thanks to all of you for your patience.
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
Originally posted by Harish Paravasthu:
public class Test{
public static void main(String[] args)
{
Object a = new Object();
// the object original referenced by object reference a
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c;
c=b;
b=a;
d=null;
}
}

A small change to the above code...here in this case,how many objects go into garbage collected???

i think only one object...


Mhhh. I think two objects are eligible for the garbage collector.
By assigning d=c, the object created by "Object d = new Object()" gets lost and by assignung d=null, the object created by "Object c = new Object" gets lost.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Part of what makes this tricky is the line that looks like:

x = y = z;

I don't think this bit of syntactic weirdness is on the exam. Does any SCJPer out there remember seeing that construct?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Dan Longest
Greenhorn

Joined: Apr 28, 2005
Posts: 5
The meaning of multiple assignment is not hard to decipher when you remember that the assignment operator is the only binary operator that has right to left associativity.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

So as was pointed out previously,
d=c=b;
is equivalent to
c=b;
d=c;

Although I agree that doing something like that is probably not worth the confusion it could cause.

Regards,
Dan


"If I want a group decision I'll look in a mirror" - Warren Buffett
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects eligible for garbage collection.