File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another garbage collection question" Watch "Another garbage collection question" New topic
Author

Another garbage collection question

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442

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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I think there 5 object eligible for gc after line1 for the give code.
Sachin Tendulkar
Greenhorn

Joined: Sep 20, 2003
Posts: 27
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: Jun 20, 2002
Posts: 442
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

Joined: Mar 20, 2001
Posts: 210

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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jun 20, 2002
Posts: 442
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

Joined: Nov 22, 2008
Posts: 18944
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
 
Consider Paul's rocket mass heater.
 
subject: Another garbage collection question
 
Similar Threads
Garbage Collection
Question 8 Valentin's mock
garbage collection
What does it mean to be eligible for gc?
New SCJP mock exam