aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on garbage collection 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 "doubt on garbage collection" Watch "doubt on garbage collection" New topic
Author

doubt on garbage collection

Deepa Sobin
Greenhorn

Joined: May 22, 2011
Posts: 9
HI,

Can you help with the below question?

class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3; //6
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}
After line 6, how many objects are eligible for garbage collection?
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Deepa,
welcome to Javaranch!!...
Firstly, please use 'Code' tags (you will find it at the top of the editor) to place your programs, this makes it easier to read and resolve doubts,
however I will do that for you this time.



Read the codes again and reply with how many objects you think are eligible for garbage collection.

Cheers!.

Ikpefua.

OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi,
I think only one object will be eligible for a GC(the one that is created inside a method and assigned to reference c2)


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Vijay...I Reserve my comments to see what Deepa has to say regarding your choice.
Deepa Sobin
Greenhorn

Joined: May 22, 2011
Posts: 9
Hi Ikpefua,

Thanks for formatting the code. I also think that one object will be eligible for garbage collection, which was created within the method. But the answer for this question was marked as "2 objects are eligible for garbage collection" without giving any explanation. Hence the confusion.

Regards
Deepa
Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

Hi! i think its 2 objects because 1) c1 will be assigned new object after processing of m1(c1) and hence its original object is ready for gc. 2) c2 also lost original object after reassignment.


Thanks & Regards
Prashanth

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Deepa wrote:
But the answer for this question was marked as "2 objects are eligible for garbage collection"


Hello Deepa,
to the BEST of my knowledge, this program as-it-stands, has ONE object eligible for garbage collection, which is as you and Vijay earlier mentioned, the object created in line 12 of the program is eligible for garbage collection as a consecuence of the assignment operation that takes effect AFTER the execution of line 8.

Regards

Ikpefua.

PS: Please you are REQUIRED to quote the source of programs you post in this forum.

Deepa Sobin
Greenhorn

Joined: May 22, 2011
Posts: 9
Hi Ikpefua,

Thanks for the help. The object referenced by c1 is still valid as c1 is still referring to it.
I found this question in http://www.examsguide.com

Regards
Deepa
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Prashanth Patha wrote:Hi! I think its 2 objects because 1) c1 will be assigned new object after processing of m1(c1) and hence its original object is ready for gc. 2) c2 also lost original object after reassignment.

Prashanth, when you invoke the method in line 6, reference variables 'ob1' AND 'c1' refer to the Object in line 5, but when line 12 executes, 'ob1' is now RE-ASSIGNED to the Object created in line 12. Hence 'c1' is still alive and connected to its Object.

REMEMBER that it is a 'COPY' of the 'BITS' in the reference variable 'c1' that is actually passed into the methods argument at the time of invocation.

Cheers!

Ikpefua.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

One more thing I want to advise you guys on, when it comes to garbage collection, an image is worth a thousand words, click on this LINK and observe how I used an easy diagram to resolve a rather 'trickish' garbage collection question right here in this forum. Diagram drawing saves you from the 'brain stress' garbage collection can give sometimes.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Ikpefua Jacob-Obinyan wrote:One more thing I want to advise you guys on, when it comes to garbage collection, an image is worth a thousand words, click on this LINK and observe how I used an easy diagram to resolve a rather 'trickish' garbage collection question right here in this forum. Diagram drawing saves you from the 'brain stress' garbage collection can give sometimes.


I agree completely. Drawing a diagram is the only way I can tackle these problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on garbage collection