aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection doubt 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 "Garbage collection doubt" Watch "Garbage collection doubt" New topic
Author

Garbage collection doubt

Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hi all,
This doubt am posting though already a thread have detailed discussion. But the doubt is still there so am starting it afresh. have a look-:
Pradeep Singh-:
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]


Dean Jhones
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.


My self-:
Hi,

quote:
--------------------------------------------------------------------------------
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-:

quote:
--------------------------------------------------------------------------------
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

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.



The detailed discussion on this is here- Click Here

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection doubt