Make heap exhaustion more robust in art-tests

Bug: 144525957
Test: art/test/testrunner/testrunner.py
Change-Id: I182f8f3c6f3f6c352a7c3846e4bfb6ff9f639953
diff --git a/test/044-proxy/src/OOMEOnDispatch.java b/test/044-proxy/src/OOMEOnDispatch.java
index 75574e7..00110bc 100644
--- a/test/044-proxy/src/OOMEOnDispatch.java
+++ b/test/044-proxy/src/OOMEOnDispatch.java
@@ -27,6 +27,7 @@
     static ArrayList<Object> storage = new ArrayList<>(100000);
 
     private static void exhaustJavaHeap(int size) {
+      Runtime.getRuntime().gc();
       while (size > 0) {
         try {
           storage.add(new byte[size]);
@@ -51,13 +52,13 @@
         // OOME to prevent GC thrashing, even if later allocations may succeed.
         Runtime.getRuntime().gc();
         System.runFinalization();
-        Runtime.getRuntime().gc();
+        // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here.
 
         int initial_size = 1024 * 1024;
         // Repeat to ensure there is no space left on the heap.
         exhaustJavaHeap(initial_size);
-        exhaustJavaHeap(/*size*/ 8);
-        exhaustJavaHeap(/*size*/ 8);
+        exhaustJavaHeap(/*size*/ 4);
+        exhaustJavaHeap(/*size*/ 4);
 
         try {
             inf.foo();
diff --git a/test/064-field-access/src/OOMEOnNullAccess.java b/test/064-field-access/src/OOMEOnNullAccess.java
index e854b1b..fbcb613 100644
--- a/test/064-field-access/src/OOMEOnNullAccess.java
+++ b/test/064-field-access/src/OOMEOnNullAccess.java
@@ -37,6 +37,7 @@
     static ArrayList<Object> storage = new ArrayList<>(100000);
 
     private static void exhaustJavaHeap(int size) {
+      Runtime.getRuntime().gc();
       while (size > 0) {
         try {
           storage.add(new byte[size]);
@@ -56,13 +57,13 @@
         // OOME to prevent GC thrashing, even if later allocations may succeed.
         Runtime.getRuntime().gc();
         System.runFinalization();
-        Runtime.getRuntime().gc();
+        // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here.
 
         int initial_size = 1024 * 1024;
         // Repeat to ensure there is no space left on the heap.
         exhaustJavaHeap(initial_size);
-        exhaustJavaHeap(/*size*/ 8);
-        exhaustJavaHeap(/*size*/ 8);
+        exhaustJavaHeap(/*size*/ 4);
+        exhaustJavaHeap(/*size*/ 4);
 
 
         try {
diff --git a/test/080-oom-throw/src/Main.java b/test/080-oom-throw/src/Main.java
index a579c21..cd36eff 100644
--- a/test/080-oom-throw/src/Main.java
+++ b/test/080-oom-throw/src/Main.java
@@ -54,6 +54,7 @@
     }
 
     private static int exhaustJavaHeap(Object[] data, int index, int size) {
+        Runtime.getRuntime().gc();
         while (index != data.length && size != 0) {
             try {
                 data[index] = new byte[size];
@@ -72,7 +73,8 @@
         // OOME to prevent GC thrashing, even if later allocations may succeed.
         Runtime.getRuntime().gc();
         System.runFinalization();
-        Runtime.getRuntime().gc();
+        // NOTE: There is a GC invocation in exhaustJavaHeap. So we don't need one here.
+
         while (result == null && size != 0) {
             try {
                 result = new Object[size];
@@ -84,8 +86,8 @@
             int index = 0;
             // Repeat to ensure there is no space left on the heap.
             index = exhaustJavaHeap(result, index, size);
-            index = exhaustJavaHeap(result, index, /*size*/ 8);
-            index = exhaustJavaHeap(result, index, /*size*/ 8);
+            index = exhaustJavaHeap(result, index, /*size*/ 4);
+            index = exhaustJavaHeap(result, index, /*size*/ 4);
         }
         return result;
     }
diff --git a/test/617-clinit-oome/src/Main.java b/test/617-clinit-oome/src/Main.java
index eeeea5e..bab344f 100644
--- a/test/617-clinit-oome/src/Main.java
+++ b/test/617-clinit-oome/src/Main.java
@@ -16,15 +16,13 @@
 
 public class Main {
   private static int exhaustJavaHeap(Object[] data, int index, int size) {
-    while (true) {
+    Runtime.getRuntime().gc();
+    while (size > 0) {
         try {
             data[index] = new byte[size];
             index++;
         } catch (OutOfMemoryError e) {
             size /= 2;
-            if (size == 0) {
-                break;
-            }
         }
     }
     return index;
@@ -40,14 +38,14 @@
         // OOME to prevent GC thrashing, even if later allocations may succeed.
         Runtime.getRuntime().gc();
         System.runFinalization();
-        Runtime.getRuntime().gc();
+        // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here.
 
         int index = 0;
         int initial_size = 256 * 1024 * 1024;
         // Repeat to ensure there is no space left on the heap.
         index = exhaustJavaHeap(data, index, initial_size);
-        index = exhaustJavaHeap(data, index, /*size*/ 8);
-        index = exhaustJavaHeap(data, index, /*size*/ 8);
+        index = exhaustJavaHeap(data, index, /*size*/ 4);
+        index = exhaustJavaHeap(data, index, /*size*/ 4);
 
         // Initialize now that the heap is full.
         Other.print();