OSDN Git Service

Add test case for runFinalization.
authorMathieu Chartier <mathieuc@google.com>
Thu, 4 Sep 2014 21:15:35 +0000 (14:15 -0700)
committerMathieu Chartier <mathieuc@google.com>
Thu, 4 Sep 2014 22:18:34 +0000 (15:18 -0700)
There was a bug causing runFinalization to return before recently
freed objects were finalized. This is a regression test for this bug.

Bug: 17381967
Change-Id: Ide6e2037685324423e83965fae3935f3e7f0aba6

test/036-finalizer/expected.txt
test/036-finalizer/src/Main.java

index a2a74fc..36fa5f8 100644 (file)
@@ -11,3 +11,4 @@ gc + finalize
 sleep
 reborn: [FinalizerTest message=nothing, finalized=false]
 wimp: null
+Finalized 1024 / 1024
index 328425f..390472d 100644 (file)
@@ -120,6 +120,39 @@ public class Main {
 
         System.out.println("reborn: " + FinalizerTest.mReborn);
         System.out.println("wimp: " + wimpString(wimp));
+        // Test runFinalization with multiple objects.
+        runFinalizationTest();
+    }
+
+    static class FinalizeCounter {
+      private static Object finalizeLock = new Object();
+      private static volatile int finalizeCount = 0;
+      private int index;
+      static int getCount() {
+        return finalizeCount;
+      }
+      FinalizeCounter(int index) {
+        this.index = index;
+      }
+      protected void finalize() {
+        synchronized(finalizeLock) {
+          ++finalizeCount;
+        }
+      }
+    }
+
+    private static void runFinalizationTest() {
+      int count = 1024;
+      Object[] objs = new Object[count];
+      for (int i = 0; i < count; ++i) {
+        objs[i] = new FinalizeCounter(i);
+      }
+      for (int i = 0; i < count; ++i) {
+        objs[i] = null;
+      }
+      System.gc();
+      System.runFinalization();
+      System.out.println("Finalized " + FinalizeCounter.getCount() + " / "  + count);
     }
 
     public static class FinalizerTest {