This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on garbage collection

 
Deepa Sobin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Vijay...I Reserve my comments to see what Deepa has to say regarding your choice.
 
Deepa Sobin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic