Re: finalize() newbie question

From: Doug Pardee (dougpardee_at_yahoo.com)
Date: 03/25/04


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.



Relevant Pages

  • Re: VB 6.0 Desktop - Global Variables
    ... variables are not "slower" than local variables. ... wide scope", then this kind of variables only can live in a module. ... standard code module. ... without needing a reference to an object. ...
    (microsoft.public.vb.general.discussion)
  • Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
    ... when in fact it's the other local variables that they (via the ... anonymous delegates) reference that are of concern. ... and "expensive" are tied in lifetime by their being in the same scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Private variables question
    ... already in use at a higher scope. ... but if those three lines reference a variable that is declared ... Local variables are always named and referenced with a lower case ... help in such cases even if you have a prefix. ...
    (borland.public.delphi.non-technical)
  • Re: missing include by compiler
    ... "Charles Russell" wrote: ... > block of code and reference it by name as in pascal. ... stay in scope due to host association and you can comment out the call ... In addition you can have local variables if you ...
    (comp.lang.fortran)
  • Re: vbs output to txt file
    ... the reference count to that object will be decremented. ... As does going out of scope:) ... You are religiously setting objects to nothing and not ... and so am seriously lacking in both scripting expertise ...
    (microsoft.public.scripting.wsh)