Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another garbage collection question

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How many objects are eligible for garbage collection immediately after line 1?
Ans is 3
My question is ,are all the objects created in method eligible for garbage collection once when the method completes?How about when you pass the object as an argument to some method from the method?How about object that is used in the return statement?for eg assuming dosomething() declaration is modified to return some object,uncommenting line 2 ,line 3,how many objects will be eligible for garbage collection then?
Thanks
Veena
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there 5 object eligible for gc after line1 for the give code.
 
Sachin Tendulkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Barkhat. There are 5 objects eligible and not 3 after line 1.
Two TechnoGC objects
Two Object objects
and
One Object[] array object.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like all the objects created in doSomething() are eligible for GC. The method doesn't return any reference, and nothing is called from it. Instance member tGC is not nulled and still alive. I wonder how they counted 3.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barkat Mardhani:
I think there 5 object eligible for gc after line1 for the give code.


How 5?Can anybody explain in detail with commented lines included & excluding the commented lines?
Veena
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited to make it easier to read, see the comment for the object and
the count number. This is just as java lova said.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All object created in this method are eligible for GC as method returns nothing back.
public void doSomething(){
TechnoGC tgc = new TechnoGC(); // first
TechnoGC tgc1 = new TechnoGC(); // second
Object[] objArray = new Object[2]; // third -- the array object
for(i = 0 ; i < objArray.length ; i++) {
objArray[i] = new Object(); // forth and fifth
}
}
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barkat Mardhani:
All object created in this method are eligible for GC as method returns nothing back.
public void doSomething(){
TechnoGC tgc = new TechnoGC(); // first
TechnoGC tgc1 = new TechnoGC(); // second
Object[] objArray = new Object[2]; // third -- the array object
for(i = 0 ; i < objArray.length ; i++) {
objArray[i] = new Object(); // forth and fifth
}
}

Barkat,
What if method is returning something?For ex can you explain what happens when method is returning something?I mean if I change the code as follows

Now how many objects are eligible for garbage collection after line 1?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case What happens wiht the references of doSomething()?
Are the reference tgc elegible for GC?
i think so, because the argument of somemeth() take the object, but what happen wiht the reference tgc1?
If i am wrong plz correct me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic