Re: Garbage collection problem

From: Lee Fesperman (firstsql_at_ix.netcom.com)
Date: 03/02/04


Date: Tue, 02 Mar 2004 21:51:43 GMT

John C. Bollinger wrote:
>
> There are some subtleties involved in determining eligibility for GC in
> Java, the most notable being "hidden" local variables. Hidden local
> variable arise because Java does not actually have nested local variable
> scopes at the VM level -- only at the Java source level. To the VM all
> local variables are treated equally. Therefore, a local reference
> variable that goes out of scope in the Java sense sticks around until
> the method in which it is declared terminates, no matter how long that
> may be. Any object it refers to remains strongly reachable until that time.

That is no quite true. At the bytecode level, local variables are 'slots' in the stack
frame. Some compilers will reuse the slots allocated to nested local variables thus
removing the reachability of references in the reused slots.

This only applies to direct interpretation of bytecodes. Runtime compilers are free to
apply additional optimizations, including reuse of 'unnested' local variables. I have
seem JVMs that perform even more esoteric optimizations.

> That there are other GC models where the answer might be a bit
> different, perhaps including some in which the problem is not a trick
> question. For Java, however, the bottom line answer is "there is no
> such reason."

For Java, it is best to assume that (as you said) a local variable remains reachable
until the method terminates. However, it is not guaranteed.

-- 
Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)


Relevant Pages

  • Re: Newbie (Post#1): Help with Array of Objects
    ... In java, unlike member ... > variables, local variables are not implicitly initialized, and you cannot ... sense if you picture yourself in a war against the compiler to make your ... The code always fails at runtime. ...
    (comp.lang.java.help)
  • Re: "Staggering power of closures..."
    ... Peter Seibel wrote: ... > them and under the covers calls a constructor with the local variables ... "Low overhead" is I guess the way ... > (The nearest translation in Java would involve making count a mutable ...
    (comp.lang.lisp)
  • Using final with local variables
    ... A question for the Java gurus. ... That makes sense because the compiler is directly writing machine code ... and can make use of the hint if it wants to. ... I am talking about local variables, ...
    (comp.lang.java.programmer)
  • Re: SQL and the LGP-30
    ... What needs to happen is a tighter integration of Java and SQL. ... Then you could do compile time checking to make sure column names are ... correct and the types of local variables could be generated from the ... SQL allows fully dynamic work that could not be type checked, ...
    (comp.lang.java.databases)
  • Re: Garbage collection problem
    ... > these methods can have arguments or local variables that are references to ... These references are said to belong to a root set of references ... This is obviously about reachability. ... > declared in a local variable declaration statement immediately contained ...
    (comp.lang.java.programmer)