8068260: java/io/Serializable/clearHandleTable/ClearHandleTable.java timed out
authorchegar
Thu, 05 Mar 2015 09:38:45 +0000
changeset 29271 162fe03153a4
parent 29270 0f65e3c44659
child 29272 3363c00f4a4e
8068260: java/io/Serializable/clearHandleTable/ClearHandleTable.java timed out Reviewed-by: rriggs, smarks
jdk/test/java/io/Serializable/clearHandleTable/ClearHandleTable.java
--- a/jdk/test/java/io/Serializable/clearHandleTable/ClearHandleTable.java	Wed Mar 04 15:05:41 2015 -0800
+++ b/jdk/test/java/io/Serializable/clearHandleTable/ClearHandleTable.java	Thu Mar 05 09:38:45 2015 +0000
@@ -30,37 +30,48 @@
 import java.io.*;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 
 public class ClearHandleTable {
+    private static final int TIMES = 1000;
+
     public static void main(String[] args) throws Exception {
         final int nreps = 100;
         ObjectOutputStream oout =
             new ObjectOutputStream(new ByteArrayOutputStream());
-        WeakReference[] refs = new WeakReference[nreps];
+        List<WeakReference<?>> refs = new ArrayList<>(nreps);
 
         for (int i = 0; i < nreps; i++) {
             String str = new String("blargh");
             oout.writeObject(str);
-            refs[i] = new WeakReference(str);
+            refs.add(new WeakReference<Object>(str));
         }
 
         oout.reset();
-        exhaustMemory();
+
+        int count = 0;
+        for (int i=0; i<TIMES; i++) {
+            // relying on, possibly multiple, System.gc calls to clear weak references
+            System.gc();
 
-        for (int i = 0; i < nreps; i++) {
-            if (refs[i].get() != null) {
-                throw new Error("failed to garbage collect object " + i);
+            Iterator<WeakReference<?>> itr = refs.iterator();
+            while(itr.hasNext()) {
+                WeakReference<?> ref = itr.next();
+                if (ref.get() == null) {
+                    itr.remove();
+                }
             }
+            if (refs.isEmpty())
+                break;
+            Thread.sleep(20);
+            count++;
+            if (count % 10 == 0)
+                System.out.println("Looping " + count + " times");
         }
-    }
 
-    static void exhaustMemory() {
-        ArrayList blob = new ArrayList();
-        try {
-            for (;;) {
-                blob.add(new int[0xFFFF]);
-            }
-        } catch (OutOfMemoryError e) {
+        if (!refs.isEmpty()) {
+            throw new Error("failed to garbage collect object");
         }
     }
 }