JavaMemoryModel: Out-of-scope variables and finalization

From: Doron Rajwan (doron@rajwan.org)
Date: Fri Oct 17 2003 - 17:59:28 EDT


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