Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4217804

Different behaviour using Reference when running with and without JIT

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.2.0
    • vm-legacy



      Name: krT82822 Date: 03/05/99


      The following code works as expected when setting JAVA_COMPILER
      to NONE. The expected behavior is that the Ref objekt eventually
      gets finalized and the PhantomReference gets added to the
      ReferenceQueue. When using the default JIT, the for loop keeps
      looping. The reference held by the PhantomReference never
      gets finalized. If I change the line towards the end of the
      loop which sets 'r = null;' to 'r = new Ref(i++);' the object
      pointed to by the PhantomReference gets finilized and garbage
      collected.

      import java.lang.ref.*;

      public class Ref {

        protected static ReferenceQueue refQ;

        protected int id;
        protected byte[] b;
        protected volatile static Ref r;

        public static void main(String[] args) throws Exception {
          byte[] b;
          PhantomReference pRef;
          Reference test;
          int i = 0;

          refQ = new ReferenceQueue();
          pRef = new PhantomReference(r = new Ref(i++), refQ);

          for(;;) {
            b = new byte[200000];
            test = refQ.poll();
            if(test != null)
              break;
            r = null;
          }
        }

        public Ref(int id) {
          this.id = id;
          this.b = new byte[100000];
        }
        public String toString() {
          return "Ref: " + id;
        }
        protected void finalize() throws Throwable {
          System.out.println("finilizing: " + this);
          super.finalize();
        }
      }
      (Review ID: 54005)
      ======================================================================

            duke J. Duke
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: