Do more GCs for test 141

Try to prevent rare race conditions that could cause the class loader
to not be unloaded.

Bug: 36377932
Test: test/testrunner/testrunner.py --host -t 141

Change-Id: I9429350b73c241478d474af37262576a512cc6a6
diff --git a/test/141-class-unload/src/Main.java b/test/141-class-unload/src/Main.java
index 595c70d..7e8431f 100644
--- a/test/141-class-unload/src/Main.java
+++ b/test/141-class-unload/src/Main.java
@@ -59,7 +59,7 @@
         // Stop the JIT to ensure its threads and work queue are not keeping classes
         // artifically alive.
         stopJit();
-        Runtime.getRuntime().gc();
+        doUnloading();
         System.runFinalization();
         BufferedReader reader = new BufferedReader(new FileReader ("/proc/" + pid + "/maps"));
         String line;
@@ -83,12 +83,20 @@
         }
     }
 
+    private static void doUnloading() {
+      // Do multiple GCs to prevent rare flakiness if some other thread is keeping the
+      // classloader live.
+      for (int i = 0; i < 5; ++i) {
+         Runtime.getRuntime().gc();
+      }
+    }
+
     private static void testUnloadClass(Constructor<?> constructor) throws Exception {
         WeakReference<Class> klass = setUpUnloadClassWeak(constructor);
         // No strong references to class loader, should get unloaded.
-        Runtime.getRuntime().gc();
+        doUnloading();
         WeakReference<Class> klass2 = setUpUnloadClassWeak(constructor);
-        Runtime.getRuntime().gc();
+        doUnloading();
         // If the weak reference is cleared, then it was unloaded.
         System.out.println(klass.get());
         System.out.println(klass2.get());
@@ -98,7 +106,7 @@
         throws Exception {
       WeakReference<ClassLoader> loader = setUpUnloadLoader(constructor, true);
       // No strong references to class loader, should get unloaded.
-      Runtime.getRuntime().gc();
+      doUnloading();
       // If the weak reference is cleared, then it was unloaded.
       System.out.println(loader.get());
     }
@@ -110,7 +118,7 @@
         Throwable throwable = (Throwable) stackTraceMethod.invoke(klass);
         stackTraceMethod = null;
         klass = null;
-        Runtime.getRuntime().gc();
+        doUnloading();
         boolean isNull = weak_klass.get() == null;
         System.out.println("class null " + isNull + " " + throwable.getMessage());
     }
@@ -118,7 +126,7 @@
     private static void testLoadAndUnloadLibrary(Constructor<?> constructor) throws Exception {
         WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor);
         // No strong references to class loader, should get unloaded.
-        Runtime.getRuntime().gc();
+        doUnloading();
         // If the weak reference is cleared, then it was unloaded.
         System.out.println(loader.get());
     }
@@ -147,7 +155,7 @@
 
     private static void testNoUnloadInstance(Constructor<?> constructor) throws Exception {
         Pair p = testNoUnloadInstanceHelper(constructor);
-        Runtime.getRuntime().gc();
+        doUnloading();
         // If the class loader was unloded too early due to races, just pass the test.
         boolean isNull = p.classLoader.get() == null;
         System.out.println("loader null " + isNull);