Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt on garbage collection

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You can't have everything. Where would you put it?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic