wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection (Cyclic references) Also known as Object Island Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection (Cyclic references) Also known as Object Island" Watch "Garbage Collection (Cyclic references) Also known as Object Island" New topic
Author

Garbage Collection (Cyclic references) Also known as Object Island

peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
Hello i was viewing some posts on cyclic References and Object island and thought tha i knew it.
Well when i tried some mock exams i understood that i haven't understand it well
So i tried to do some drawing since this Helps a lot to understand these kind of questions .
Looks like i was a Crap Artist
"not sure what i do"

How can i draw diagram that can help me figure which objects are elligable for garbage collection ?
This tough


Time is relative so there is no way i can be late.
~Albert Einstein~
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
With a pencil and paper, using line and arrows? With the code broken into chunks or something?

Lines 04-06
mac --> (mac Computer, computer --> null) //A silly little convention, hopefully you can follow
pc --> (pc Computer, computer --> null)
ar --> (ar Computer, computer --> null)

Lines 07-08
mac --> (mac Computer, computer --> pc Computer)
pc --> (pc Computer, computer --> ar Computer)
ar --> (ar Computer, computer --> null)

Line 09
mac --> (mac Computer, computer --> pc Computer)
pc --> (pc Computer, computer --> ar Computer)
ar --> (mac Computer, computer --> pc Computer) //ar was set to mac

Line 10
mac --> (mac Computer, computer --> pc Computer) //There was no chnage to mac, mac.computer was already pc
pc --> (pc Computer, computer --> ar Computer)
ar --> (mac Computer, computer --> pc Computer) //This is the same ref as mac!

Line 11
mac --> null
pc --> (pc Computer, computer --> ar Computer)
ar --> (mac Computer, computer --> pc Computer)

Line 12 (ARGH!)
mac --> (pc Computer, computer --> ar Computer) //ar.computer was assigned to
pc --> (pc Computer, computer --> ar Computer) //Assigned mac to pc
ar --> (mac Computer, computer --> pc Computer) //No change

Line 13
mac -->(pc Computer, computer --> ar Computer)
pc --> (pc Computer, computer --> ar Computer)
ar --> null

Ergo, one is eligible. Can you see why?


SCJP6
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I need to see what the Computer class looks like. You are assigning instance variables.


JDBCSupport - An easy to use, light-weight JDBC framework -
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
It's the same class as you see !
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Sebastian, they provided the Computer class. it a simple object with a main and one default scope member of type Computer.

btw My answer has been corrected - I'd missed a .computer (D'oh!)
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
hey Thanks A lot !
I will study it !
i was confused with the Devaka questions at the Exam Lab
with Garbage collection
i will try this aproach to see if i can answer the questions
WOW !!! you did it HAHAHAHA
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
For a proper question I follow a simplified form of UML. Quite often the questions say "When will the object created on line 1 be eligible for gc?"
So I'd start with something like...And then as those refs change, I'd cross them out (numbering the cross/new ref so I know which line did it) and drawing any new refs.
So lets way line 3 is "objA = objB;", then I would have I can quickly see that nothing refers to the first Object and it was line 3 that did it.
I guess many people will have their own little notation for doing this.

Did you work out which one was eligible in your question?
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
basically i wrote this to experiment a bit with my self
How can i use this kind of approach on questions like this ?
(Sorry for my English ) GREECE
DEVAKA EXAM LAB

it says that 7 are eligable for Garbage collection
I can only see 4
"We live in very hard, but also strange world.
Today someone must be very brave in order to be coward…"

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

The number of objects gc'd is 7.


a1 -----> object(a1--->null, a2----->null)
a2 -----> object(a1--->null, a2----->null)
a1.a1 = new A() => a1 -----> object(a1--->object(a1---->null,a2----->null), a2----->null)
a2.a2 = new A() => a2 -----> object(a1--->null), a2--->object(a1---->null,a2----->null))
a1.a2=new A().a1 =>a1 -----> object(a1--->object(a1---->null,a2----->null), a2----->null) because a1.a2 = object(a1---->null)
a2.a2=a1 => a2 ----->object(a1--->object(a1---->null,a2----->null), a2----->object(a1--->null,a2----->null)) 1 object eligible for gc here
well next 2 lines 2 more objects are created....But see only a1 and a2 can be used to reference from a live thread. So all the objects created are eligible for gc 2(from last 2 lines)+1(line above)+4 (from a1.a1, a2.a2 and a1.a2) = 7


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537



in this problem what will happen if we have a line called Computer hybrid = new Computer().computer = new Computer(); ?? how is it assigned and will the object created here be available for gc??
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Assuming that compiles (it's late, I've been studying all day and I am too tired to think about it hard) then it will not be eligible for garbage collection.

hybrid(stack) --> new Computer, computer(heap) --> other new Computer

Although "other new Computer" is not referenced from the stack, the object enclosing the reference is.
If "hybrid" become null or it pointed to something else, and nothing else refers to "new Computer" or "other new Computer"; then they are both eligible.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
peter kosmas wrote:it says that 7 are eligable for Garbage collection
I can only see 4

This one is simple. Just count the number of "new2 keywords. Why? Well, when a1 and a2 go out of scope, they will effectively sever all connection with the stack.
Even though a1.a1 may refer to an object, a1.a1 lives out on the heap and doesn't count for preventing garbage collection unless a1 (or an other identical reference) still exists on the stack.
Ergo, no a1 or a2 any more then what they referred to is eligible, a1.a1, a1.a2, a1.a1.a2... etc are all eligible as stated above.
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
HEY ! THIS REALLY HELPED !
I'll waste no time reading it.
))
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Computer hybrid = new Computer().computer = new Computer();


What i think happens is new Computer() object is formed and the way to get to that object is stored in next object new Computer().computer reference variable. This bit representing to get to new Computer() is then passed on to Computer hybrid leaving new Computer().computer being gc'd.

a1.a2=new A().a1;


about this i think its the same as the object is formed with a1 = null. This null reference is then passed on to a1.a2 which then actually is nulled rather than referring to new A().a1
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Nitish Bangera wrote:
a1.a2=new A().a1;


about this i think its the same as the object is formed with a1 = null. This null reference is then passed on to a1.a2 which then actually is nulled rather than referring to new A().a1


Ye Nitish, bud don't forget that difference between


and


is that in the first case, one object will be eligible for GC as an effect of the new A() statement (I didn't read the whole thread so don't know if you already know this )...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
I tried this aproach it works but it took me 20 minutes
hope i wont have this kind of question on the exam

a1 -----> object(a1--->null, a2----->null)
a2 -----> object(a1--->null, a2----->null)
a1.a1 = new A() => a1 -----> object(a1--->object(a1---->null,a2----->null), a2----->null)
a2.a2 = new A() => a2 -----> object(a1--->null), a2--->object(a1---->null,a2----->null))
a1.a2=new A().a1 =>a1 -----> object(a1--->object(a1---->null,a2----->null), a2----->null) because a1.a2 = object(a1---->null)
a2.a2=a1 => a2 ----->object(a1--->object(a1---->null,a2----->null), a2----->object(a1--->null,a2----->null)) 1 object eligible for gc here
well next 2 lines 2 more objects are created....But see only a1 and a2 can be used to reference from a live thread. So all the objects created are eligible for gc 2(from last 2 lines)+1(line above)+4 (from a1.a1, a2.a2 and a1.a2) = 7
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
There will be.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

I'll show how I create memory maps for questions like this. So if I create a map for the original program at statement 8, then it would be like this



(my damn slow internet is not allowing me to preview my posts, I have to edit them over and over again )
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Actually GC questions are really easy if you don't rush through and yes if you know how the diagrams are formed. With a pen and paper they are really easy. While drawing, just remember use the right to left strategy. like Dog g = new Dog(); So a Dog object is created and assign all its instance variables to the default values first and then assign it to g. Now its a reference so it will contain only the bits(which i dunno what they are and what size they will be) representing to get to that Dog object.

an ankit its not the internet speed......javaranch people are doing something i guess because though i have a real fast internet package, the only webpage that's creating a problem is javaranch.
peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
that's true i could not click on the link of the SCJP Faq so i suppose by changing the objectives in the SCJP they must changing the SCJP Faq i am not sure !
i wanted to write
That it's easy using these diagrams
but how can someone Represent those diagrams
when you have question like
A.A.a1 ?
you put them inside the diagrams ?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

first one will alwayz be the reference variable then on its all instance variables separated by the "."
Teven Chan
Greenhorn

Joined: Jun 08, 2009
Posts: 6
Thinking about where to reside for the local variable and instance variable, should be helpful for figuring out which the object is eligible for GC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection (Cyclic references) Also known as Object Island