# Garbage collection

Henry Zhi Lin
Ranch Hand
Posts: 69
Guys,

this is from devaka's examlab, practice 1, question 68.

the answer is after line 9 we have only one object eligible to GB.

Can anyone draw a diagram for me I am confused here again?

Henry Wong
author
Marshal
Posts: 21711
85
Can anyone draw a diagram for me I am confused here again?

Instead of just trusting the answer, can you tell us what you think the answer should be? After all, I am assuming that you are confused here because you think the answer is something else?

(hint: I don't agree with the answer either)

Henry

Henry Zhi Lin
Ranch Hand
Posts: 69
Herny,

the following is what I thought,

after line 6
a1 ---> OA
a2 ---> OB
a3 ---> OC

after line 7
a1 ---> OA --> OD
a2 ---> OB
a3 ---> OC

after line 8
a1 ---> OA --> OD
a2 ---> OB --> OD
a3 ---> OC

after line 9
a1 ---> OA --> OD
a2 ---> OB --> OD
a3 ---> OC --> OD

after line 10
a2 ---> OB --> OD
a3 ---> OC --> OD
which OA should be eligible to GB, so my answer is after line 10 not line 9.

Devaka Cooray
Marshal
Posts: 4750
361
Hi Henry Zhi Lin,

Originally posted by Henry Zhi Lin:

...the answer is after line 9 we have only one object eligible to GB...

But in my simulator, the correct answer as given is "line 10" (Answer D)! However another time you said,....
Originally posted by Henry Zhi Lin:

...so my answer is after line 10 not line 9....

So, if both you and me said that the correct answer is line 10; then who said, the answer is after line 9?

Due to your posts, others may think that the questions/answers in my simulator are not accurate; as you can see below:
Originally posted by Henry Wong:

hint: I don't agree with the answer either

Because of this, please feel kind to correct this as soon as possible.

Devaka.

Henry Zhi Lin
Ranch Hand
Posts: 69
Sorry Devaka, I guess I must misread your answer. I have a suggest on your simulator, would you mind to add save test result function, currently after I exit your simulator everything will not be saved.

Thanks anyway!

Fabio Nascimento
Greenhorn
Posts: 16

After line 10

After line 10 you have one Aegis Object eligible for GC, after all you have 4 objects eligible for GC, three that you removed the references manually and one that become unreachable after that.

Rekha Srinath
Ranch Hand
Posts: 178
All,

Sometimes I get confused on such situations...

In this code, the 'ags' of a1 is pointing to another Aegis object (lets call it X). Ok..

Now when a1 is made null, there is no meaning for a1.ags, right? which means that object X cannot be reached via a1.ags.

So, if I have the code as:
Aegis a1=new Aegis();
a1.ags=new Aegis();
a1 = null; // Line 1

After Line 1, there are two objects eligible for GC, right?

Please correct me if I am wrong.

James Tharakan
Ranch Hand
Posts: 580
@ Rekha Srinath

Hope the following tell you are Right

Output:
Games
James

[ November 19, 2008: Message edited by: James Tharakan ]

Rekha Srinath
Ranch Hand
Posts: 178
That was a cool example James !!

Jyothsna Panchagnula
Ranch Hand
Posts: 113
Hi James,

Can we say that Fun f and f.f1 are the objects that are garbage collected. ?

thanks,
Jyothsna

James Tharakan
Ranch Hand
Posts: 580
Yes the objects refered BY f anf f.f1

Edmen Tay
Ranch Hand
Posts: 39
So, if I have the code as:
Aegis a1=new Aegis();
a1.ags=new Aegis();
a1 = null; // Line 1

So, Can we said that when object reference a1 point to null, then its instance variable reference(a1.ags) will be set to null as well. Am I right?

Please correct, if i am wrong.

Thanks

Regards,
Edmen