File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt about GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt about GC" Watch "doubt about GC" New topic
Author

doubt about GC

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
This is from Devaka Coorey's Diagonistic test(Examlab).

class A {
A aob;

public static void main(String args[])
{
A a= new A();
A b= new A();
A c= new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d= new A().aob=new A();
c=b;
c.aob=null;//

System.gc();

}}

Question :- after c.aob how many oblects are eligible for GC.

Answer:- (given) is 2.


But i tried with graph, all the time i am getting only one object (c).
Which one is other?

Thanks,
Geeta Vemula.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Indeed there are two object eligible. This is what I think

The first object is the one referenced originally by C.

A a= new A();
A b= new A();
A c= new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d= new A().aob=new A();
c=b; //(1)

It becomes eligible at (1).

The other object eligible for GC is at statement

A d= new A().aob=new A();

here the object created in the text in bold will be eligible for GC. The object in italicized text will be assigned to d.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
can you giv explanation for A d=new A().aob=new A(); ?

is nt object referenced by d.aob?


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Originally posted by Ganeshkumar cheekati:
can you giv explanation for A d=new A().aob=new A(); ?

is nt object referenced by d.aob?


Statement: A d=new A().aob=new A();

Note, above statement can be written as below:
A d= (new A().aob=new A());

That means, the bracketed expression will be evaluated at first. Inside this expression, a new object will be created by the below italic expression:
A d= (new A().aob=new A());

And then, another object will be created by the following bold expression, and it will be assigned to the "aob" attribute of the above object:
A d= (new A().aob=new A());

Finally, the result of the above bracketed expression will be assigned to the variable 'd'. Note, the result of the bracketed expression is the above bold object. Then the object in italic mode is eligible to GC.
Vinayatha Kumar
Greenhorn

Joined: Nov 10, 2008
Posts: 3
Ankit/Kenneth - Thanks for your explanations! I have one more question on this.

If I modify the Statement:

A d=new A().aob=new A();

to:

new A().aob=new A(); // case 1 - without assigning the object to a reference variable

Further if I modify this to:

new A().aob= a; // case 2 - assign an existing object reference.

Would new A() (object in italics) be eligible for GC in both case 1 and case 2 ? I assume it would be, since we would not be able use this object without a reference variable.

Can any of you please tell me if I am correct?
[ November 13, 2008: Message edited by: Vinayatha Kumar ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

You are right Vinayatha. In both the cases, the new A() will be eligible for GC. in the first case, both the objects will be eligible for GC...
Vinayatha Kumar
Greenhorn

Joined: Nov 10, 2008
Posts: 3
Oh, yes. I missed that about the other object in case 1. Thanks for pointing out.
Bhumeshwar Narsayya
Greenhorn

Joined: Jan 22, 2008
Posts: 2
Hello everyone. I hope this example may clear the doubts. It obviously cleared mine.

public class GarbageTest{
private GarbageTest obj;
private String name;
public GarbageTest(String name){
this.name=name;
}
public static void main(String...args){
GarbageTest g=new GarbageTest("One").obj=new GarbageTest("Two");
System.out.println(g.name);
}
}

Here the output is Two instead of One. This shows that new GarbageTest("One") may be eligible for gc

Anyways... I am going for SCJP6 exam tomorrow. I'll let the result of mine known. cheers
[ November 16, 2008: Message edited by: Bhumeshwar Narsayya ]
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
Can any one tell me how "c=b" becomes eligible for garbage collection?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Originally posted by Jyothsna Panchagnula:
Can any one tell me how "c=b" becomes eligible for garbage collection?


c was assigned an object at this statement

A c= new A();

then when c = b is executed, the object that c originally pointed to (the one that I have bolded) becomes eligible for GC...
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
Hello Ankit ,

Thanks for your input.
My doubt is cleared.

thanks,
Jyothsna
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
public class GarbageTest{
private GarbageTest obj;
private String name;
public GarbageTest(String name){
this.name=name;
}
public static void main(String...args){
GarbageTest g=new GarbageTest("One").obj=new GarbageTest("Two");
System.out.println(g.name);
}
}


in this problem if asked how many objects is eligible for Garbage Collection, will the answer be one, the object created by new GarbageTest("One") or three coz the constructor of GarbageTest creates an object name and also there is an object GarbageTest obj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt about GC