diff options
author | 2020-07-23 17:41:10 -0700 | |
---|---|---|
committer | 2020-08-13 22:21:19 +0000 | |
commit | 2b2a7505df3bf4ea418da0496dbc07e18f8ccd00 (patch) | |
tree | 1814b47d1b8c4f599f5957c184fb6c8f104788d2 | |
parent | 87d6987b89e36d71842dc40869fb365462125a34 (diff) |
Make heap exhaustion robust in app-image-methods test
It's need as we now throw OOME even when a small percentage of heap is
still free. This change ensures that the heap is fully exhausted, which
is expected by the test.
Bug: 144525957
Test: art/test/testrunner/testrunner.py
Change-Id: Id607423e7c250bdae9b05b9eb2f5b8571ca906e1
-rw-r--r-- | test/163-app-image-methods/src/Main.java | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/test/163-app-image-methods/src/Main.java b/test/163-app-image-methods/src/Main.java index 33590fc450..2b57634896 100644 --- a/test/163-app-image-methods/src/Main.java +++ b/test/163-app-image-methods/src/Main.java @@ -62,9 +62,30 @@ public class Main { } } + private static int exhaustJavaHeap(Object[] data, int index, int size) { + Runtime.getRuntime().gc(); + // Let out-of-bound exception be thrown if we go past the array length. This should + // never happen if the logic in the caller is right. The exception acts as an assertion. + while (size != 0) { + try { + data[index] = new byte[size]; + ++index; + } catch (OutOfMemoryError oome) { + size /= 2; + } + } + return index; + } + 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(); + // 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]; @@ -74,14 +95,10 @@ public class Main { } if (result != null) { int index = 0; - while (index != result.length && size != 0) { - try { - result[index] = new byte[size]; - ++index; - } catch (OutOfMemoryError oome) { - size /= 2; - } - } + // Repeat to ensure there is no space left on the heap. + index = exhaustJavaHeap(result, index, size); + index = exhaustJavaHeap(result, index, /*size*/ 4); + index = exhaustJavaHeap(result, index, /*size*/ 4); } return result; } |