Win a copy of Rust Web Development this week in the Other Languages 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Objects eligible for garbage collection.

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls have a look at the following code:
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Answer given in the explanation of this code is 3. Shouldn't it be 4.

If 3 then please explain how.

Thanks & regards.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Harish Paravasthu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but could not understand. If possible could you pls elaborate.
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic