Win a copy of Fixing your Scrum this week in the Agile 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

help in garbage collection question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


I got this question from examsguide.com free sample questions...
please help
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first an objectod C is created which is referenced by c1.
then c1 is passed into method m() which does nothing but creates another C object that is referenced by c2
and finally a thierd object referenced by c3 is created.
when line c2=c3 is done..
it simply makes c2 refer to the object also referenced by c3..
so there is no reference left to the object that was originally referenced by c2.
so only 1 object is eligible for GC.
hope it helps...
 
Amrita Chaurasia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot
 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur tyagi wrote:first an objectod C is created which is referenced by c1.
then c1 is passed into method m() which does nothing but creates another C object that is referenced by c2
and finally a thierd object referenced by c3 is created.
when line c2=c3 is done..
it simply makes c2 refer to the object also referenced by c3..
so there is no reference left to the object that was originally referenced by c2.
so only 1 object is eligible for GC.
hope it helps...



I dont think your answere is correct. After line#6 there are no objects eligible for garbage collection. Ref. variables c1,c2,c3 are still refrencing C object.
 
Sheriff
Posts: 9686
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I dont think your answere is correct. After line#6 there are no objects eligible for garbage collection. Ref. variables c1,c2,c3 are still refrencing C object.


Well there is 1 object eligible for GC. The explanation is correct. c1,c2 and c3 do refer to an object of class C, but the object that c2 pointed to originally will be eligible for GC...
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ankit, you are correct, i was just misunderstood.

The object which got created inside the static method lost its reference to c3, which inturn pointing to a different C object.

 
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just arranged it for a better view.

Naveen
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amrita Chaurasia wrote:
After line 6, how many objects are eligible for garbage collection?



It's a trick question.

You don't know because you don't know how many eligible objects have already been GCed.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulrika,


Please explain your idea about it being a trick question!

Thanks,

Bert

p.s. I don't think it is, because that's how Sun tends to phrase this kind of question, but maybe you've got an insight here...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranches,

There will possibly be two objects that will be getting garbage collected, c1 and c2 (old reference,obj1) will be the unlucky ones.


Cheers
Rajanish
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic