• 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

GC Question.

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

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: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
en, three objects created in the program
c1,c3,ob1

after line 6
ob1 is unreachable ,hence ob1 is eligible for gc
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amisha,

Changchun is correct. After line 6 the object reference created from within m1 method and returned to C2 variable cannot be reached anymore.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Amisha,
Only one object will be ready for garbage collection.
Wen you assign c2=c3, the reference to ob1 is lost and
its now ready for collection, garbage collection.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amisha,

3 objects become eligble for gc, first c3 dies due to end of method,

c1 dies in method m1 method


the object created in m1 method i.e obj1 dies in main method due to c2=c3.

Hope it is clear...
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kumar,

I don't think you're correct. The question asks how many objects will be eligible for GC after line 6

After such line the main method() is still running and have not ended.
[ April 17, 2006: Message edited by: Edisandro Bessa ]
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok sorry i have not read question properly, in general that is the answer if we consider upto line 6 then you are right
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Edisandro
you are correct only object created in m method eligible for gc
 
Amisha Shah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

here , method is returning object so how that object is eligible for garbage colection ? i read in k&b at page 434(Reassining a reference variable) object return by method is not eligible for garbage collection.

so i think , there is no object eligible for garbage collection. is there anyone who think so ?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No amisha u r wrong...
yeah ur partially correct...method is returning the object reference and is set to c2 ..so at that time object created in m1 method is not eligible for garbage...Then have a look at this code..

C c3=new C();
c2=c3; //6
so a new object referenced by c3 is created..and then c2 is made to refer to this new created object....

So the object previously refered by c2 become eligible for gabrage collection...

Hope it would have Cleared

-----------------------
Magesh.s
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic