File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt about GC

 
geeta vemula
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you giv explanation for A d=new A().aob=new A(); ?

is nt object referenced by d.aob?
 
Kenneth Lomvey
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, yes. I missed that about the other object in case 1. Thanks for pointing out.
 
Bhumeshwar Narsayya
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me how "c=b" becomes eligible for garbage collection?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ankit ,

Thanks for your input.
My doubt is cleared.

thanks,
Jyothsna
 
Jacob Sonia
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic