• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

doubt about GC

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9701
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you giv explanation for A d=new A().aob=new A(); ?

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

Thanks for your input.
My doubt is cleared.

thanks,
Jyothsna
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic