aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finalize() and GC 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 "Finalize() and GC" Watch "Finalize() and GC" New topic
Author

Finalize() and GC

Mayuri Lakhani
Greenhorn

Joined: Nov 18, 2002
Posts: 28
Hi all,
Can anybody pl. tell me what is required to know about finalize method for the exam and explain in brief? It is keeping me confused.
One more thing related to GC
public class a
{
public static void main(String abc[])
{
Object newobject = new object();
/* Now some operations..nothing related to the newobject.
So when newobject becomes eligible for garbage collection??
Immediately after it is created? I dont think this should be the case b'coz at anytime i can need this object. Or it becomes eligible once main is compleated. May be I am missing something very fundamental*/
}
}
Thanks
Mayuri
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy --
For finalize(), you need to know the circumstances under which it is called, and what it really means.
For example, you should know that there is no guarantee that finalize() will be called before your program ends (because the GC may not run).
The finalize() method IS guaranteed to be called, ONCE and only once, before an object is collected. So *if* the garbage collector DOES decide to reclaim your object, then it is guaranteed that your finalize() method WILL be called.
One potentially tricky thing: remember that if finalize() is called, an object can STILL come back to life, by passing itself (foo.saveMyLife(this)) to some method that will in turn keep the reference. In that case, finalize() is called, but the object is NOT reclaimed, and even if it later becomes eligible again the finalize() method will never be called again! (well, for the life of that VM instance).
So it's possible that the object becomes eligible, then the GC runs and selects the object for death, and calls the object's finalize(), then the object (inside its finalize()) brings itself back to life, then LATER the object becomes eligible again, the GC runs and once again selects the object for death, but THIS time the object is simply (silently) reclaimed without finalize() being called again.
I think that's about it for the exam.
As for your other question, an object becomes eligible for GC when it is no longer reachable from a live thread. So that could mean:
* The reference goes out of scope because it is a local variable and the method completes (we don't look at gc scoping below the level of a method)


The reference is assigned another object:

* The reference is nulled out:


There's also the case of "islands of isolation" Bert talks about, where two objects are referring to each other, but no live thread can reach EITHER of them. In that case, they are both eligible. So do not be fooled into thinking that as long as an object has a reference to some other object, that referenced object can't become eligible. Garbage collection rules are not governed by the number of references that exist, only by the number of references that can actually be USED.
garbage collection questions are fun, and there is a surpisingly high number of them on the test. So it's worth taking the time to be sure you've got it. GC and multi-dimensional array questions are the two kinds of questions where I have to draw pictures or I can't figure out what's going on. When I first took the exam, that was mostly what I used my two exam pieces of paper for. (well, that and AWT layouts, but now you don't have to worry about that!)
Cheers,
Kathy
author of the bestsellers, "Who moved my Garbage Collector?" and "Chicken Soup for the Garbage Collecting Soul"
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313


So when newobject becomes eligible for garbage collection??
Immediately after it is created? I dont think this should be the case b'coz at anytime i can need this object. Or it becomes eligible once main is compleated. May be I am missing something very fundamental

You got it -- its eligible once main is completed. 'cause at any point during the main... you may start using it.
what about this one -- after which line will 'j' and 'k' be eligible for GC?

[ January 06, 2003: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Mayuri Lakhani
Greenhorn

Joined: Nov 18, 2002
Posts: 28
Hi Kathy,
Thanks a lot for the wonderful explaination, I wish I could have read your book before my exam.
. I am taking exam tomm.
Hi Jess,
Pl. tell me did I follow Kathy correctly?
J should be eligible for garbage collection after line 3 b'coz its out of scope here.
And K should be eligible for GC after ling 8 i.e. after this method completes its execution.
Thanks all
Mayuri
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Mayuri Lakhani:

J should be eligible for garbage collection after line 3 b'coz its out of scope here.
And K should be eligible for GC after ling 8 i.e. after this method completes its execution.

yes and yes
relax, take a deep breathe, you'll do fine on the exam. Check out this excellent thread for some last minute hints.
sam huang
Greenhorn

Joined: Dec 31, 2002
Posts: 28
Hi all,
I am very confused why j and k are eligible for GC?
I don't think so because I think only object can be garbage collected.
Am I right?
Please tell me know.
Sam


SCJP,SCWCD,SCBCD,MCSE
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
Hi,
May be I'm wrong but I thought that only objects (stored on the heap) are candidate for garbage collection. Normal variables (primitives & references) are on the stack and they live & die as per normal rules of scope n stuff.
So in the example that Jess showed, GC has nothing to do with j & k?
One more doubt: When GC is invoked, is it a guarantee that all the objects that are candidates are reclaimed by it OR it is possible that only some of them will be collected?
Thanks,
deep
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I agree with Sam and Deep. The term "garbage collection" is never user to refer to primitive types, as far as I know. Garbage collection refers to the system for automatically reclaiming memory on the heap, which is occupied by objects. OK these objects may have reference data inside them, but in this case we still refer to GC of the whole object, not just part of it. And in the code Jessica shows, j and k are local variables, stored on the stack, not the heap.
It is true that the memory used by these variables may be freed up at the stated line numbers. In practice though the JVM usually just waits for the end of the method, and frees all the local variables at once. Unless the JIT compiler performs some funny tricks. Ask Bill Brogden about this; I'm sure he just loves to discuss it.
(Not really - this was the subject of a mock exam question Bill wrote that turned out to be far more complex than anticipated, and he still gets e-mail from people confused about it..)
One more doubt: When GC is invoked, is it a guarantee that all the objects that are candidates are reclaimed by it OR it is possible that only some of them will be collected?
There is no guarantee.


"I'm not back." - Bill Harding, Twister
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Originally posted by Jessica Sant:

yes and yes

Jessica, do you argee that GC is only performed on objects? It was a surprize question to me too and thought you posed a trick question... seems not. Primitive garbage? Thought we evolved to complicated garbage ... or something

Please confirm, someone, if you would (otherwise I'd have to research it myself..... and it seems the ranchers are allowed to be a bit lazy sometimes (or maybe there time is just limited, lik mine)). Thanks.


<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Neither j nor k are ever garbage collected. Being primitive local variables, they are allocated on the stack when you enter the method and de-allocated when you exit it. Garbage collection only works on the heap, not on the stack. And don't worry, this is not exam material
Now if Jess had used Iterators for the for loops...
Also agree with Jim that you get no garbage collection guarantees. In fact, modern JVMs tend to use these fancy generational garbage collectors that classify objects into two or more different categories; some categories get garbage collected much more aggressively and much sooner than others. So garbage collection can be very complicated and it's best not to make assumptions. In fact the language specification tells you in so many words not to make assumptions. The only guarantee you get is that garbage collection will be performed before the JVM throws an OutOfMemoryError at you.
- Peter
[ January 07, 2003: Message edited by: Peter den Haan ]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Peter den Haan:
Neither j nor k are ever garbage collected. Being primitive local variables, they are allocated on the stack when you enter the method and de-allocated when you exit it. Garbage collection only works on the heap, not on the stack. And don't worry, this is not exam material
Now if Jess had used Iterators for the for loops...

oops -- bad Jess...
I'll just quietly go sit in the corner now... *sniff*
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Originally posted by Jessica Sant:

oops -- bad Jess...
I'll just quietly go sit in the corner now... *sniff*

I think people should get banned for 'told you so attitude', but fortunately the crowd here wouldn't do that... as far as I know. Just thought you might know some funky secrets or something.
Cheers.

Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Jessica Sant:
I'll just quietly go sit in the corner now... *sniff*
Don't sit there for too long --- you might be garbage collected
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finalize() and GC
 
Similar Threads
GC needs help !
finalize() not only for GC eligible Objects?
Garbage collection..
Self Written Code on Garbage Collection
GC question 2