Make tests robust against GCs failing early
044-proxy, 064-field-access, 080-oom-throw, and 617-clinit-oome
all intentionally run out of memory, and then try to run a
test with a full heap. In the presence of aosp/1286274, this can fail
if we initially had some empty space in the heap. A GC may recover
< 1% of space, in which case we return OOME. But later allocations
may still succeed.
The fix is to make sure there is no empty space in the heap when
we start. If we end up performing a forced GC, the heap was completely
full. Since no memory is dropped during this process, the heap will
still be full when we finish.
Test: testrunner.py --host -t 044-proxy &
Test: testrunner.py --host -t 080-oom-throw
Change-Id: I10e3f77cd16d25af96751d039e2e498e1cb43315
diff --git a/test/044-proxy/src/OOMEOnDispatch.java b/test/044-proxy/src/OOMEOnDispatch.java
index 2ee5792..b6fb3e8 100644
--- a/test/044-proxy/src/OOMEOnDispatch.java
+++ b/test/044-proxy/src/OOMEOnDispatch.java
@@ -37,6 +37,12 @@
Main.stopJit();
Main.waitForCompilation();
+ // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw
+ // OOME to prevent GC thrashing, even if later allocations may succeed.
+ Runtime.getRuntime().gc();
+ System.runFinalization();
+ Runtime.getRuntime().gc();
+
int l = 1024 * 1024;
while (l > 8) {
try {
diff --git a/test/064-field-access/src/OOMEOnNullAccess.java b/test/064-field-access/src/OOMEOnNullAccess.java
index 9343cd6..9960241 100644
--- a/test/064-field-access/src/OOMEOnNullAccess.java
+++ b/test/064-field-access/src/OOMEOnNullAccess.java
@@ -42,6 +42,12 @@
Main.stopJit();
Main.waitForCompilation();
+ // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw
+ // OOME to prevent GC thrashing, even if later allocations may succeed.
+ Runtime.getRuntime().gc();
+ System.runFinalization();
+ Runtime.getRuntime().gc();
+
int l = 1024 * 1024;
while (l > 8) {
try {
diff --git a/test/080-oom-throw/src/Main.java b/test/080-oom-throw/src/Main.java
index 3d5d062..4750119 100644
--- a/test/080-oom-throw/src/Main.java
+++ b/test/080-oom-throw/src/Main.java
@@ -56,6 +56,11 @@
public static Object eatAllMemory() {
Object[] result = null;
int size = 1000000;
+ // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw
+ // OOME to prevent GC thrashing, even if later allocations may succeed.
+ Runtime.getRuntime().gc();
+ System.runFinalization();
+ Runtime.getRuntime().gc();
while (result == null && size != 0) {
try {
result = new Object[size];
diff --git a/test/617-clinit-oome/src/Main.java b/test/617-clinit-oome/src/Main.java
index 94cb7ce..bee6e01 100644
--- a/test/617-clinit-oome/src/Main.java
+++ b/test/617-clinit-oome/src/Main.java
@@ -20,6 +20,13 @@
Object[] data = new Object[100000];
try {
System.out.println("Filling heap");
+
+ // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw
+ // OOME to prevent GC thrashing, even if later allocations may succeed.
+ Runtime.getRuntime().gc();
+ System.runFinalization();
+ Runtime.getRuntime().gc();
+
int size = 256 * 1024 * 1024;
int index = 0;
while (true) {