RE: JavaMemoryModel: Out-of-scope variables and finalization

From: Evan Ireland (eireland@sybase.com)
Date: Sun Oct 19 2003 - 16:25:27 EDT


Doron,

Considering that the javadocs state:

public static void runFinalization()

  Runs the finalization methods of any objects pending finalization.

    Calling this method suggests that the Java Virtual Machine expend effort
    toward running the finalize methods of objects that have been found to
    be discarded but whose finalize methods have not yet been run.

I can't see any requirement that the last value (or even all 10 values)
of 'ft' have been "found to be discarded", just because of the System.gc()
call. A conservative garbage collector, for example, might believe that
some or all of the values are not garbage.

> -----Original Message-----
> From: owner-javamemorymodel@cs.umd.edu
> [mailto:owner-javamemorymodel@cs.umd.edu]On Behalf Of Doron Rajwan
> Sent: Saturday, 18 October 2003 10:59 a.m.
> To: javaMemoryModel@cs.umd.edu
> Subject: JavaMemoryModel: Out-of-scope variables and finalization
>
>
> In JDK 1.1 I had the following problem (that I cannot reconstruct in JDK
> 1.4.2_01):
>
>
>
> Consider the code below. It seems that it should finalize all FinTest
> objects, but it was NOT the case back then. When viewing the generated
> bytecodes, the variable "ft" seems to live as long as the method lives,
> thus, the last instance of FinTest was not finalized (see bytecode 16).
>
>
>
> Under the new semantics, I cannot find anything that prevents it from
> happening. Also, nullifying "ft", which solved the problem back then,
> can be removed by the optimizer, according to the new semantics. Making
> "ft" volatile is illegal by the JLS (and would be ignored anyway, since
> only single thread accesses local variables).
>
>
>
> Can someone please tell me how it was fixed in JDK 1.4?
>
> Does it mean that any variable going out-of-scope is nullified? Does it
> hurt performance?
>
> Does it work well in all other JVMs?
>
>
>
> Thanks,
>
> Doron.
>
>
>
>
>
> public class Loop {
>
> public static class FinTest {
>
> public FinTest(int index) {
>
> this.index = index;
>
> }
>
> protected void finalize() {
>
> System.out.println(index);
>
> }
>
> private final int index;
>
> }
>
>
>
> public static void main(String[] args) {
>
> for (int i = 0; i < 10; ++i) {
>
> FinTest ft = new FinTest(i);
>
> }
>
> System.gc();
>
> System.runFinalization();
>
> System.out.println("Done");
>
> }
>
> }
>
>
>
>
>
> public static void main(java.lang.String[]);
>
> Code:
>
> 0: iconst_0
>
> 1: istore_1
>
> 2: iload_1
>
> 3: bipush 10
>
> 5: if_icmpge 23
>
> 8: new #2; //class Loop$FinTest
>
> 11: dup
>
> 12: iload_1
>
> 13: invokespecial #3; //Method Loop$FinTest."<init>":(I)V
>
> 16: astore_2
>
> 17: iinc 1, 1
>
> 20: goto 2
>
> 23: invokestatic #4; //Method java/lang/System.gc:()V
>
> 26: invokestatic #5; //Method
> java/lang/System.runFinalization:()V
>
> .
>
>
>
> _______________________________________________
>
> Doron Rajwan, doron@rajwan.org, www.rajwan.org <http://www.rajwan.org/>
>
> 13 Odem street, Rishon Le-Zion, Israel
>
> Home: 03-9512348 [+972-(3)-9512348]
>
> Cell: 064-887732 [+972-(64)-887732]
>
>
>
>

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:51 EDT