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

Garbage collection

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

OCP Java SE 6 Programmer practice exam --> practice exam 2 --> Question No 50



I can't understand why the option D is correct , because at line 18 there is one object (f1) for garbage collection.

Thanks in advance.


OCPJP 6 (91%)
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Ankit Gareta wrote:OCP Java SE 6 Programmer practice exam --> practice exam 2 --> Question No 50


Option D is correct because all the objects still have valid references. You have to draw a memory diagram to easily evaluate the circumstances though! Just going by lines of code will be smartly confusing


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4372
    
    8

Ankit Gareta wrote:I can't understand why the option D is correct , because at line 18 there is one object (f1) for garbage collection.

Remember, f1 isn't an object, it's a reference. References are never garbage collected, objects are. It's a picky distinction, but it does emphasise that you need to keep track of all the references.

Rajdeep is right - drawing a diagram is by far the best way of doing this type of question.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575


Usually I debug using after overriding finalize method.[not perfect solution though, it will workout to understand]
1.just place above statement in 18 th line and comment/un comment line from 11 to 16 randomly and see carefully how many are Garbage collected and *why*?
2. then, move the while to line 8 and see now.

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
The diagram is :
f1 -> object 1
base -> object 1
f2 -> object 2
object 1.f -> object 2
f2.f -> object 1
f3-> object1.f

After line 18,
base -> null , f1 ->null, f2-null,
But f3-> object 2, f3.f-> object 1

Object 1 and 2 have references refering to them. So, no objects are eligible for GC.
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks for your reply Rajdeep, Matthew, Seetharaman and Himai.

Himai Minh wrote:The diagram is :
f1 -> object 1
base -> object 1
f2 -> object 2
object 1.f -> object 2
f2.f -> object 1
f3-> object1.f

After line 18,
base -> null , f1 ->null, f2-null,
But f3-> object 2, f3.f-> object 1

Object 1 and 2 have references refering to them. So, no objects are eligible for GC.


I'm still confuse in that how f3.f points to object 1 (becouse there is no f3.f=___ assignment) ?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
Ankit,
f3 refers to object1's f object, which is object2.
f3.f refers to object2.

The diagram is :
object 1.f -> object 2
f3-> object1.f

So , f3 refers to object1.f, which refers to object 2

Also,
f2->object2
f2.f->object1,
So, object2.f refers to object1

So, f3 refers to object 2, object2.f refers to object1.

Both object 1 and 2 are referred and not eligible for GC.

When dealing with this kind of questions, identify the object created, not the reference refering to the object. In this program, three objects are created: object1 (Fiji() created in ling 11) , object2 (Fiji() created in line 13), new Fiji () in line 7.



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Himai Minh wrote:
f2->object2
f2.f->object1,
So, object2.f refers to object1
object2.f refers to object1.
Both object 1 and 2 are referred and not eligible for GC.

f2.f ----> object1
if f2 assigned to null, then the object which pointed by f2 eligible for garbage collection and will be garbage collected.
now, there is no f because when f2 dies f also die. So, objectc1 will be garbage collected if there is no other references to object1.

you can see this behavior if you analyze code like I pointed above.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Please ignore this reply, Himai's explanation is pretty clear!
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
Seetharaman Venkatasamy wrote:
Himai Minh wrote:
f2->object2
f2.f->object1,
So, object2.f refers to object1
object2.f refers to object1.
Both object 1 and 2 are referred and not eligible for GC.

f2.f ----> object1
if f2 assigned to null, then the object which pointed by f2 eligible for garbage collection and will be garbage collected.
now, there is no f because when f2 dies f also die. So, objectc1 will be garbage collected if there is no other references to object1.

you can see this behavior if you analyze code like I pointed above.


If f2 is assigned to null, the object it refers is eligble for GC if no other reference refering to this object.
In the program, f3 actually refers to object2 and f3.f refers to object1.
So, that is why the answer is no object is eligible for GC after line 18.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Sorry about that Himai I re-read your diagram and edited my post.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
Praveen Kumar M K wrote:Please ignore this reply, Himai's explanation is pretty clear!


Parveen Kumar MK,

To answer your question "how to access the object Fiji (100)?" Try to print f3.f.f to see if it is that object.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
Praveen Kumar M K wrote:Sorry about that Himai I re-read your diagram and edited my post.


You don't need to say sorry. If you have any questions, please continue to post it. Thanks.
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks Himai for your explanation.

I understand the things. Here f3 points to f1.f which points to f2 , so indirectly f3 points to f2.
so indirectly f2.f is the same as f3.f that points to f1, so both the object is pointed when line 18 reached.

Thank all guys for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection