GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many objects will be 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 "how many objects will be eligible for garbage collection " Watch "how many objects will be eligible for garbage collection " New topic
Author

how many objects will be eligible for garbage collection

pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Hi
For below code ,answer is given 2 But i want to know why not 4 objects are eligible for garbage collection because in every object there is a reference variable z of class type which have reference of other object.So when two outer objects that is c1 & c2 created at line 1 and line 2 become eligible for garbage collection then why not their inside objects also become eligible for garbage collection.

And my second question is that when I run this program on jdk 1.6 ,finalize method is not called .WHY? I think finalize method is not gurranted to run.Am I right?


/*
how many objects will be eligible for garbage collection when reached //to the call to f()method.
*/

class myclass
{
private myclass z;
public void other(myclass c){z=c;}
protected void finalize()
{
System.out.println("called");
}

}


class garbagetest2
{
private static void f()
{
}
public static void main(String[]args)
{
myclass c1=new myclass ();//line 1
myclass c2=new myclass();//line 2
c1.other(c2);
c2.other(c1);
myclass c3=new myclass();
c1=c3;
c2=c3;
f();
}

}
[source is Whizlab software]


SCJP 5.0(75%), SCWCD 5.0(88%)
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
In effect you have 4 reference variables but they're pointing to 2 objects on the heap.

You are correct about the finalize() method - although I haven't used 1.6 to any great degree. Its up to the JVM as to when it gets called.
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Hi
Thanks !i got it.
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
There are no two internal objects inside c1 and c2 but instead object references. So only two objects are there.
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Here, c1.z =c2 and c2.z=c1. If c1 and c2 point to the obj referenced by c3 what happens to c1.z and c2.z. Can someone please explain.
[ January 08, 2008: Message edited by: Dean Jones ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Pradeep Singh,

A couple of suggestions.

  • Kindly make use of CodeTags whenever you paste the java code so that it will look neatly.
  • When you prepare for SCJP 1.5 exam, kindly use the same version of JDK and JRE. It could really avoid some confusions as every version of JDK has its own implications.


  • Good luck


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Ranchers,

    Let's have this doubt clarified with the same program with a little modification.



    The above program produces the following output:



    I think the program is self explanatory. So as the output is. Still some explanation.

  • Line 1 of output: prints the address being referred by the reference variable 'c1' which is 8567361 and its "z" variable points to 'null' initially.
  • Line 2 of output: prints the address being referred by the reference variable 'c2' which is 9584176 and its "z" variable points to 'null' initially.
  • Line 3 of output shows both the addresss of c1 and c1.z,since before this line c1.z is assigned with c2. Refer to line 3 in program. that's why you get the value of c1=8567361 and c1.z=9584176.
  • Line 4 of output is same as line 3. You get the value of c2=9584176 and c2.z=8567361.
  • Line 5 of output shows the address of c3 and c3.z. as c3 is newly created and c3.z is NOT being assigned to anything initially, you get the output as : c3=19972507 and c3.z=null.
  • Line 6 and 7 shows the output of c1 and c2 reference variables after they are assigned with c3. Refer line 6 and 7 in the program.


  • As the complete reference variable "c1" is changed to point to the location which is pointed by "c3", the output is c1=19972507 and c1.z=null.

    Once c1 is made point to the same location as that of c3, c1.z is also should be same as that of c3.z. right? Since c3.z is NOT assigned to anything and it is null, c1.z is also null here. [refer line 5 in output]

    Same case with c2 as well.


    As such, the objects being pointed to by c1 and c2 reference variables are no longer in existence and being referred by any other variables, these two objects are eligible for garbage collection.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    One Twist:

    Try changing the program by assigning c3.z to c1 or c2 prior to line 6 and 7 in the above program.



    Now can you say how many objects are eligible for garbage collection?
    Dean Jones
    Ranch Hand

    Joined: Dec 29, 2007
    Posts: 129
    Brilliant explanation Raghavan. Thanks a lot!

    Answer to the twist, ie, is only 1 object will be eligible for garbage collection. If c3.z=c1, then the object initially referenced by c2 is eligible and vice-versa.

    Am I right?
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by Dean Jones:
    .. then the object initially referenced by c2 is eligible and vice-versa.

    Am I right?


    Yes. That's true. You can check in your output as well.

    But please keep in mind that the question is asking *eligible for garbage collection*. and NOT actually garbage collected as it is NOT determined and guaranteed about the timings!
    Arjun Cheng
    Greenhorn

    Joined: Jan 08, 2008
    Posts: 13
    I think is zero. Because c1.z hold c2, and c2.z hold c1 and you put anyone of c1 and c2 into c3.z. c1, and c2 still have reference to them.

    Am I?
    Dean Jones
    Ranch Hand

    Joined: Dec 29, 2007
    Posts: 129
    Arjun , you are wrong because, Once c1 is made point to the same location as that of c3, c1.z is also should be same as that of c3.z. right? Since c3.z is NOT assigned to anything and it is null, c1.z is also null here.
    Pranav Bhatt
    Ranch Hand

    Joined: Mar 20, 2006
    Posts: 284
    Hi,
    As per Dean
    Here, c1.z =c2 and c2.z=c1. If c1 and c2 point to the obj referenced by c3 what happens to c1.z and c2.z. Can someone please explain.


    I guess he meant to ask what will happen to the previous c1.z and c2.z before reassgning c1 and c2 to c3. As i was suspecting they are still pointing to same objects hashcode as they previously were. Though the after c1 and c2 points to c3 the new z references got a value of null.But here's a quick question, if the original ref z still pointing to previous objects then how garbage collector will work on them as they still have a reference though an internal one

    Please refer the following changes i have done to prog-:

    package mocks;

    class myclass {
    private myclass z;

    public void other(myclass c) {
    z = c;
    }

    protected void finalize() {
    System.out.println("called");
    }

    public String toString() {
    return "[" + this.hashCode() + "]";
    }

    public myclass getZ() {
    return this.z;
    }
    }

    class GarbageTest {
    private static void f() {
    }

    public static void main(String[] args) {
    myclass c1 = new myclass();// line 1
    System.out.println("c1 is : " + c1 + ", c1.z = " + c1.getZ());
    myclass c2 = new myclass();// line 2
    System.out.println("c2 is : " + c2 + ", c2.z = " + c2.getZ());
    c1.other(c2); // line 3
    System.out.println("(2) c1 is : " + c1 + ", c1.z = " + c1.getZ());
    c2.other(c1); // line 4
    System.out.println("(2) c2 is : " + c2 + ", c2.z = " + c2.getZ());
    myclass c3 = new myclass(); // line 5
    System.out.println("c3 is : " + c3 + ", c3.z = " + c3.getZ());
    myclass c4, c5;// line 6
    c4 = c1.getZ();
    c5 = c2.getZ();
    System.out.println("c4 which is now c1.z i.e c1 ref variable z is "+ c4);
    System.out.println("c5 which is now c2.z i.e c2 ref variable z is "+ c5);

    c1 = c3; // line 7
    c2 = c3; // line 8
    System.out.println("c4 after reassigning of c1 to c3 " + c4);
    System.out.println("c5 after reassigning of c2 to c3 " + c5);

    System.out.println("(3) c1 is : " + c1 + ", c1.z = " + c1.getZ());
    System.out.println("(3) c2 is : " + c2 + ", c2.z = " + c2.getZ());
    f();
    }
    }

    I got the output as follows-:
    c1 is : [8567361], c1.z = null
    c2 is : [9584176], c2.z = null
    (2) c1 is : [8567361], c1.z = [9584176]
    (2) c2 is : [9584176], c2.z = [8567361]
    c3 is : [19972507], c3.z = null
    c4 which is now c1.z i.e c1 ref variable z is [9584176]
    c5 which is now c2.z i.e c2 ref variable z is [8567361]
    c4 after reassigning of c1 to c3 [9584176]
    c5 after reassigning of c2 to c3 [8567361]
    (3) c1 is : [19972507], c1.z = null
    (3) c2 is : [19972507], c2.z = null


    As the objects have lost the external referances and are being reffered internally so are they the victims of garbage collector? Please clarify

    [ January 09, 2008: Message edited by: pranav bhatt ]
    [ January 09, 2008: Message edited by: pranav bhatt ]
    Dean Jones
    Ranch Hand

    Joined: Dec 29, 2007
    Posts: 129
    Pranav, in this case no object is eligible for garbage collection. Raghavan please clarify.
    Pranav Bhatt
    Ranch Hand

    Joined: Mar 20, 2006
    Posts: 284
    Yes , am confused with this thing as am not getting any output for finalize method in java 1.5 itself so can't check it out myself.As the objects have lost the external references so referring by internal ones dosent make any sense, but how the things are falling into place this i need to know. Hope the elite members like Raghavan, Maha Anna,Bert Bates, Keith Lynn, Jesper Young, Burkhard Hassel and many others will calrify this soon.
    Pranav Bhatt
    Ranch Hand

    Joined: Mar 20, 2006
    Posts: 284
    Anyone please clarify this garbage collection doubt..
    Dean Jones
    Ranch Hand

    Joined: Dec 29, 2007
    Posts: 129
    Can anyone please clarify this garbage collection doubt?
    [ January 11, 2008: Message edited by: Dean Jones ]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Howdy Ranchers,

    Sorry for the delay caused. Kindly bear with me.

    Before we discuss further, have a look at the below sentence


    From the Garbage Collector chapter in K & B's book:
    If our Java program has a reference variable that refers to
    an object
    , and that reference variable is available to a live thread, then that object is considered reachable.


  • First of all, if an object does NOT have any reference variables referring to it, is definitely eligible for garbage collection. Moreover, here the reference variable is external reference variable. This indicates that the object is unreachable.


  • Next, if you have an object that is reachable via a reference variable and that reference variable is accessible to the live thread, it is NOT eligible for garbage collection.


  • Live Thread means the current thread or the thread in execution at the moment.
    .

    Hope this will be helpful. Let me make it clear with the pictures what i have made for the programs.

    I will post it here in separate posts so as to make it look neatly and legible.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    As have seen the explanations for the Original Program, lets have a look at the pictorial representation for the state of objects and their references.

    Its for the program given by the Original Poster.

    Image1: initial state of objects before reassigning the references



    [ January 11, 2008: Message edited by: Raghavan Muthu ]

    Thanks to Jan Cumps for helping me out to paste the images as he suggested here.
    [ January 12, 2008: Message edited by: Raghavan Muthu ]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Sorry people. I am unable to post any images here.

    Let me find out a way and then paste it here. .

    As such for our modified program (as what i gave),

  • c1.z refers to c2
  • c2.z refers to c1
  • c3.z refers to NULL
  • c1 and c2 are reassigned to c3.
  • Obviously, now c1.z and c2.z are NULL as c3.z was not assigned to anything explicitly.



  • For this case, the objects being referred by c1 and c2 lost their external references. Though c1.z and c2.z refer to each other, they form an island of isolation and they are eligible for GC.

    The number of objects eligible for Garbage Collection is 2.

    Though it was well explained in my earlier post itself, just to reiterate the islands of isolation, just summarized here again.

    Have a look at the same in the image below.

    Image 2: depicting the objects after reassignment.


    Hope this is clear with you.
    [ January 12, 2008: Message edited by: Raghavan Muthu ]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    For the program with the twist, as we assign the c3.z to either c1 or c2 before reassigning, the flow goes as follows:

  • c1.z refers to the object pointed by c2
  • c2.z refers to the object pointed by c1
  • c3.z refers to the object pointed by c1 (assume)


  • See the same in the pictorial form below.

    Image 3 depicting the modified form before reassigning the references

    Now, its time to look for reassigning the references!

  • c1 and c2 are reassigned to c3.
  • As such, c1, c2,c3 refers to the same object (which is created and assigned to c3 initially). c1.z and c2.z and c3.z all point to c1 (as per the step1 here above)



  • After this reassignment of references, we have one object with 3 active references (c1,c2,c3).

    See the same in the pictorial form below.

    Image 4 depicting the modified form after reassigning the references



    For the very first object which was initially pointed by c1 is having one internal reference which is via c3.z (as well c1.z and c2.z).

    Though the objects being pointed by c1 and c2 initially still have an internal reference to each other for the instance reference variable 'z', they lost their external reference.

    But still, the object initially pointed by c1 has an internal reference via c3.z now and the reference variable is available to the active thread. So it is NOT eligible for Garbage Collection.

    As per the statement given in K & B's book, i am now confused here on the object pointed by C2 here!. As such, it seems like it is still reachable via c1.z which is reachable via c3.z that is available to the active thread. As Dean Jones has initially pointed out. I guess it is still reachable only.

    We need the suggestions from experts here really. Kathy, Bert and all other masters, where are you?

    [ January 11, 2008: Message edited by: Raghavan Muthu ]

    [ January 12, 2008: Message edited by: Raghavan Muthu ]
    [ January 12, 2008: Message edited by: Raghavan Muthu ]
    sweta doshi
    Greenhorn

    Joined: Jan 04, 2008
    Posts: 14


    As per the statement given in K & B's book, i am now confused here on the object pointed by C2 here!. As such, it seems like it is still reachable via c1.z which is reachable via c3.z that is available to the active thread. As Dean Jones has initially pointed out. I guess it is still reachable only.



    Hello ,

    Does the above mean as follows :
    1. c3.other(c1) (so ..c3.z=c1.z=c2...so c3.z=c2)
    2. c1=c3 (so c1.z=c3.z=c2)
    3. c2=c3 (so c2.z=c3.z=c2 !)
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by sweta doshi:


    Hello ,

    Does the above mean as follows :
    1. c3.other(c1) (so ..c3.z=c1.z=c2...so c3.z=c2)
    2. c1=c3 (so c1.z=c3.z=c2)
    3. c2=c3 (so c2.z=c3.z=c2 !)


    Yes! except for #1.

    c3.other(c1), so c3.z=c1 and NOT c1.z!
    As such, c1.z=c2.

    But it is NOT that c3.z=c2, which is obviously wrong!
    Pranav Bhatt
    Ranch Hand

    Joined: Mar 20, 2006
    Posts: 284
    Raghavan,


    Originally posted by Raghavan:

    As per the statement given in K & B's book, i am now confused here on the object pointed by C2 here!. As such, it seems like it is still reachable via c1.z which is reachable via c3.z that is available to the active thread. As Dean Jones has initially pointed out. I guess it is still reachable only.


    I guess the object pointed by C2 can be assessed in this case. As below i have added few lines in bold and am able to access the Object2 which was previously referenced by C2. Have a look-:

    package mocks;

    class myclass {
    private myclass z;

    public myclass other(myclass c) {
    z = c;return z;
    }

    protected void finalize() {
    System.out.println("called");
    }

    public String toString() {
    return "[" + this.hashCode() + "]";
    }

    public myclass getZ() {
    return this.z;
    }

    public myclass getRemoteZ(myclass c) {
    return this.z;
    }

    }

    class GarbageTest {
    private static void f() {
    }

    public static void main(String[] args) {
    myclass c1 = new myclass();// line 1
    System.out.println("c1 is : " + c1 + ", c1.z = " + c1.getZ());
    myclass c2 = new myclass();// line 2
    System.out.println("c2 is : " + c2 + ", c2.z = " + c2.getZ());
    c1.other(c2); // line 3
    System.out.println("(2) c1 is : " + c1 + ", c1.z = " + c1.getZ());
    c2.other(c1); // line 4
    System.out.println("(2) c2 is : " + c2 + ", c2.z = " + c2.getZ());
    myclass c3 = new myclass(); // line 5
    System.out.println("c3 is : " + c3 + ", c3.z = " + c3.getZ());
    myclass c4, c5;// line 6
    c4 = c1.getZ();
    c5 = c2.getZ();
    System.out.println("c4 which is now c1.z i.e c1 ref variable z is "
    + c4);
    System.out.println("c5 which is now c2.z i.e c2 ref variable z is "
    + c5);
    c3.other(c1);
    System.out.println("c3's Z" + c3 + ", c3.z = " + c3.getZ());
    c1 = c3; // line 7
    c2 = c3; // line 8

    c5.getRemoteZ(c3.other(c1));
    System.out.println("c5's Z is now refering to the Object2 which was initially referred by C2" +" c5.z= " + c5.getZ());
    System.out.println("c4 after reassigning of c1 to c3 " + c4);
    System.out.println("c5 after reassigning of c2 to c3 " + c5);
    System.out.println(" c1 is : " + c1 + ", c1.z = " + c1.getZ());
    System.out.println(" c2 is : " + c2 + ", c2.z = " + c2.getZ());

    f();
    }
    }

    I got the following output-:

    c1 is : [8567361], c1.z = null
    c2 is : [9584176], c2.z = null
    (2) c1 is : [8567361], c1.z = [9584176]
    (2) c2 is : [9584176], c2.z = [8567361]
    c3 is : [19972507], c3.z = null
    c4 which is now c1.z i.e c1 ref variable z is [9584176]
    c5 which is now c2.z i.e c2 ref variable z is [8567361]
    c3's Z[19972507], c3.z = [8567361]
    c5's Z is now refering to the object2 which was initially referred by C2 c5.z= [9584176]
    c4 after reassigning of c1 to c3 [9584176]
    c5 after reassigning of c2 to c3 [8567361]
    (3) c1 is : [19972507], c1.z = [19972507]
    (3) c2 is : [19972507], c2.z = [19972507]


    Here it can be verified that we can reach object2 as c5.z's and original c2's object hascode are same (9584176), so i guess now no object will be eligible for Garbage collection

    [ January 12, 2008: Message edited by: pranav bhatt ]
    [ January 12, 2008: Message edited by: pranav bhatt ]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    That's true Pranav. I also could get the same.

    Hope there are zero objects eligible for GC if they have some internal references existing!
    Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8815
        
        5
    I have to confess that I didn't study this thread in detail, but I have to say that Raghavan's pictures are awesome!!!

    Great job Raghavan!


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by Bert Bates:
    ...Great job Raghavan!


    Thank you so much Bert! You made my day ! Obviously it is all the knowledge we learn and obtain from people like you.

    Infact, i thought of sending a PM to you and other bartenders to have a look at it and help us! It would be so great if you could clarify this.
    Arjun Cheng
    Greenhorn

    Joined: Jan 08, 2008
    Posts: 13
    That getRemoteZ is great. I think every one is clear now.


    Thanks!
    Ram Siddharth
    Greenhorn

    Joined: Mar 21, 2011
    Posts: 1
    Awesome explanation
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how many objects will be eligible for garbage collection