Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC Question.

 
Amisha Shah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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?
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • 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.
 
vivek yadav
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
mohamed ewis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
magesh sripathy
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic