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

About GC

 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is the Object o, eligible for gc ?

I guess the answer should be "after the test() method completes" (because the method test() does not return the reference to outside), am I right?

Thanks in advance!
Jack
 
Panos Liaskos
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack,
I don't think that the object referenced by the o reference variable is eligible for gc.
The only object that is eligible for gc is the one referenced by the obj reference variable (line: obj = new Object() .
After the method completes the local method reference variable obj is out of scope (so it doesn't exist outside the scope) but there is still in memory the object that it refers. That object is eligible for GC.
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then when would Object o eligible for gc ?
If I add the following code, will Object o eligible for gc ?

Thanks,
Jack
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes both t and o will be eligible for g.c.
You can write a program that confirms it.

[ October 26, 2003: Message edited by: Jose Botella ]
 
Panos Liaskos
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see the hole code:

Thats what I think is happening:
1. after takeObject method completes: 1 object eligible for GC (the one referenced by obj local reference variable).
2. after test method completes: another 1 object eligible for GC (referenced by o local reference variable)
3. so after t.test() returns: 2 eligible objects.
4.next line is: t=null; Another object (of type Test) eligible for GC (referenced by t reference variable)
5. so when this line completes you have: 3 objects eligible for GC.
 
Panos Liaskos
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the exam just note on Jose's excellent (and enlighting) example that System.gc(); has no guaranteed behavior.
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!!
Jack
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic