Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection question

 
Maleen Abeydeera
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Quote Your Sources.

Thanks,
Henry
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh, I thought the exact same thing when it came to that question. Apparently we are wrong!
 
Henry Wong
author
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt get it......... :roll:
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh here's two images that I created for another similar question asked by someone





I think this will clear your doubts...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mallen is it cleared for you?

if so then can you explain it?
 
Maleen Abeydeera
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you give the diagram for this question or else your explanation?

Thanks
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit
I am clear now with your diagrams.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic