• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Garbage Collection

 
everbeen Zh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
GIVEN:
1:http://java.sun.compublic class X {
2: public void m(Object x) {
3: x = new Integer(99);
4: Integer y = (Integer)x;
5: y = null;
6: System.out.println("x is" + x);
7: }
8:}
When is the Integer object, created in line 3, eligible for garbage collection?
1)never
2)just after line 4
3)just after line 5
4)just after line 6 (that is, as the method returns)
5)when the calling method sets the argument it passed into this method to null
5) or 6)
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everbeen,
I would have to assume after line 6, but from what I understand, there is no realiable why to know when an object will be available for garbage collection.
The garbage collector runs as a background thread, when it finds memory that is no longer accessible from any active thread, it releases it back into the heap for re-use.
I hope that answers your question.
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> there is no realiable why to know when an object will be available for garbage collection.

There is no reliable way to know when an object will be collected by the garbage collector.

> 5) or 6)

You have only shown choices 1) through 5) . What is choice 6) ??

The answer is 4)just after line 6 (that is, as the method returns).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic