Re: finalize() newbie question
From: Doug Pardee (dougpardee_at_yahoo.com)
Date: 03/25/04
- Next message: Kees van Reeuwijk: "Re: [Off topic] Gay marriage"
- Previous message: Sean Russell: "Re: [Off topic] Gay marriage"
- In reply to: ark: "finalize() newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 25 Mar 2004 09:55:19 -0800
"ark" <arkhas@comcast.net> wrote:
> Could you explain why finalize() is not called when the "a" reference goes
> out of scope?
[snip]
> {
> AClass a = new AClass();
> }
> System.gc();//doesn't call finalize()
Actually, that is not a newbie question at all.
But first, a couple of things that ARE newbie issues:
1) finalize() should be avoided at all costs. Its behavior is not
fully predictable, and having even a single object that needs
finalizing seriously slows down the garbage collector.
2) the comp.lang.java newsgroup was declared obsolete in 1996,
replaced by a number of more specific newsgroups. Only a few news
servers still carry it, mainly Google and some cable ISPs. Postings to
comp.lang.java are SUPPOSED to be automatically redirected to
comp.lang.java.programmer, but some ISPs (again, mainly Google and
those same cable ISPs) fail to. See
http://groups.google.com/groups?threadm=c2htvr$k8c$1@newstree.wise.edt.ericsson.se
for the list of current Java newsgroups.
Now to the original question.
What you have there is an object that is in the "invisible" state.
Although the reference variable "a" has gone out of scope in the
SOURCE, at the binary (bytecode) level local variables aren't
allocated and deallocated as they come into and go out of scope --
they are allocated when the method is called and go away when the
method returns. So even though you can no longer access the variable
"a", the object reference still exists in the method's local
variables. It will finally disappear when the method returns.
If you were to set a=null inside the block, the object would then be
unreachable and eligible for collection.
Or if you were to have another block that had similar code, the new
variable might be assigned the same local variable slot that "a" was
(but no guarantees). When the new variable is assigned it would then
overwrite the reference to the object, and the old object would be
unreachable and eligible for collection.
See http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997414
for a discussion of "invisible" objects.
- Next message: Kees van Reeuwijk: "Re: [Off topic] Gay marriage"
- Previous message: Sean Russell: "Re: [Off topic] Gay marriage"
- In reply to: ark: "finalize() newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|