aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Garbage Collection" Watch "About Garbage Collection" New topic
Author

About Garbage Collection

everbeen Zh
Greenhorn

Joined: Dec 09, 2001
Posts: 2
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

Joined: Jul 17, 2001
Posts: 173
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

Joined: Jul 22, 2000
Posts: 9047
    
  10
> 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).

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Garbage Collection