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

Garbage collection question

Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Consider this code:

public class app {
app a;
static public void main(String[] args) {
app a1 = new app();
app a2 = new app();
app a3 = new app();
a1.a = a3;
a2.a = a1;
a3.a = a2;
a3 = null; // Line 9
a2 = null; // Line 10
a1 = null; // Line 11
}
}

At what point is only a single object eligible for GC?

well, the answer says its never.

The three Lost objects form an island of objects that refer to each other. Before line 11 runs, e is the last remaining reference to a member of the island. As long as one reference exists, the entire island is safe from the GC, but as soon as the last reference (in this case, e1) is lost, the entire island becomes eligible.


but my question is isn't the object that was originally referenced by a2 available for GC after line 10, since both its references(a2 & a3.a) are made null.

(I am assuming that when you say a3 = null, a3.a becomes null as well)

EDIT: - Source Kathy Sierra book CD
[ November 10, 2008: Message edited by: Maleen Abeydeera ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

Please Quote Your Sources.

Thanks,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Heh, I thought the exact same thing when it came to that question. Apparently we are wrong!


SCJA
~Currently preparing for SCJP6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

but my question is isn't the object that was originally referenced by a2 available for GC after line 10, since both its references(a2 & a3.a) are made null.


No. The object that was originally referenced by a2 is still referenced via a1.a.a.

Henry
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Thanks Henry,

I was thinking of a1.a as something that belonged to the a1 reference variable, not to the actual object. That seemed to be the problem.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i didnt get it......... :roll:


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Ganesh here's two images that I created for another similar question asked by someone





I think this will clear your doubts...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Maleen, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied this question from. This is not optional.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
not yet cleared ankit.


a1-----------------object1--------------a2.a

a2-----------------object2--------------a3.a

a3----------------object3---------------a1.a


when a3=null...

then a3.a also null


a1-----------------object1--------------a2.a

a2-----------------object2

object3---------------a1.a

when a2 is null
then object2 is eligible for GC


am i right?


[ November 11, 2008: Message edited by: Ganeshkumar cheekati ]
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Originally posted by Jesper Young:
Maleen, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied this question from. This is not optional.


Jasper, I have already provided the source. look at the bottom of my post.
It's the Kathy Sierra book CD
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
hi mallen is it cleared for you?

if so then can you explain it?
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Ganesh, I think you are having the same problem as I did.

The problem with your diagram is that a1.a is detached from a.
Do not think of a1.a as a separate reference variable. Think of it as something that is connected with the object a1. i.e it is part of a1 & should never be detached from it.
Ankit's diagram is great. Study it and you will get this.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes a1.a is a part of object a1.
when a1 is null then automatically a1.a sud be null...

am i right?
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Ankit,

what about e2.e=e1?. In your diagram it's not refering anything.Please explain me..
i am also confused with this.

Preparin Scjp5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Originally posted by Maleen Abeydeera:
Jasper, I have already provided the source. look at the bottom of my post.
It's the Kathy Sierra book CD

Ok, thank you. Sorry I didn't see it the first time.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Originally posted by Preetha Arun:
what about e2.e=e1?. In your diagram it's not refering anything.Please explain me..


Actually I created this diagram for some other question. This is why e2.e is not referring to anything... In this question, e2.e will point to e1... I just gave that diagram for understanding, it is not for this question specifically(as I also mentioned while providing the diagrams)...
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
could you give the diagram for this question or else your explanation?

Thanks
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yeah you mentioned it ankit.i think arun have not seen it clearly..

ankit can you explain this example clearly with diagrams?
why the answer is never?

i may thankful if you provide this.....

Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Originally posted by Ganeshkumar cheekati:
yes a1.a is a part of object a1.
when a1 is null then automatically a1.a sud be null...

am i right?


when a1 is set to null, a1.a becomes null only if there is no other reference pointing to a1. In this case there is another reference a2.a which points to a1. So a1.a can still be accessed by a2.a.a

Hope this clears it up.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

OK when
a1.a = a3;
a2.a = a1;
a3.a = a2;
is executed, the memory map looks like this



when
a3 = null;
is executed, the memory map looks like this



when
a2 = null;
is executed, the memory map looks like this



So till now no object is eligible for GC.
When
a1 = null;
is executed, the image map looks like this



So all the object become eligible for GC at once...
[ November 11, 2008: Message edited by: Ankit Garg ]
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Thanks Ankit
I am clear now with your diagrams.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection question
 
Similar Threads
method returns what happens to garbage collection
Doubt over Garbage Collection
SC Exam Lab Question 68 on GC
Garbage Collection (Cyclic references) Also known as Object Island
Confusion in Garbage Collection