File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC 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 "GC question" Watch "GC question" New topic
Author

GC question

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136

Question1: How many Object will be GCed after #1?
Question2: How many Objects will be Gced after #2
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
First of all, what do you think should be the answer? And why do you think so?
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
At #1 no object is eligible for GC. because, go() method has not finished and pudding object has also reference to this(which is new Mian at line 13).
At #2 two objects are eligible for GC. because, method is finished and there is no reference to pudding created at line 16 and also no reference variable to new Main() created at line 13.
Please correct me if I am wrong.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
That sounds right to me.

Do you know the actual answer?
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 80
at #1 as you said because
you are still inside go function.but as come out of go function the local variable which is declared inside this function(pudding) is no longer used .so it should
be GC.but here i have doubt in new Main() because you are creating an object but not assigning in any reference variable.but still jvm will have object for main.
so I think here when that obejct will be GC we cant say.corret me if somewhere i am wrong.
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 80
means gvm will check if that new Main() object is no longer used.it will be gc.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Please someone clarify this. Thank you.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Certpal question
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Janki Shah wrote:At #1 no object is eligible for GC. because, go() method has not finished and pudding object has also reference to this(which is new Mian at line 13).
At #2 two objects are eligible for GC. because, method is finished and there is no reference to pudding created at line 16 and also no reference variable to new Main() created at line 13.
Please correct me if I am wrong.

You are right
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Thank you Everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC question