diff options
author | 2020-12-23 14:32:18 -0800 | |
---|---|---|
committer | 2020-12-23 14:32:18 -0800 | |
commit | eeaf8bd989a5bbe2ffdacff5a0a3a4acece7e824 (patch) | |
tree | 38bde86015f3362ab1a0751ed90f06f4f0122ba0 | |
parent | 26de89604fdb6f7db3effcf775860f9f0dcd00f0 (diff) |
Fix java heap exhaustion in 159-app-image-fields test
Test: art/test/testrunner/testrunner.py
Bug: 144525957
Change-Id: I466e5e1a8a227ae35ccaa58b61755665192c295d
-rw-r--r-- | test/159-app-image-fields/src/Main.java | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/test/159-app-image-fields/src/Main.java b/test/159-app-image-fields/src/Main.java index 47d0116a19..e7db1e1f16 100644 --- a/test/159-app-image-fields/src/Main.java +++ b/test/159-app-image-fields/src/Main.java @@ -61,29 +61,44 @@ public class Main { } } + 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]; + ++index; + } catch (OutOfMemoryError oome) { + size /= 2; + } + } + return index; + } + public static Object eatAllMemory() { - Object[] result = null; - int size = 1000000; - while (result == null && size != 0) { - try { - result = new Object[size]; - } catch (OutOfMemoryError oome) { - size /= 2; - } - } - if (result != null) { - int index = 0; - while (index != result.length && size != 0) { - try { - result[index] = new byte[size]; - ++index; - } catch (OutOfMemoryError oome) { - size /= 2; - } - } - } - return result; - } + 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]; + } catch (OutOfMemoryError oome) { + size /= 2; + } + } + if (result != null) { + int index = 0; + // 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; + } } // The naming is deliberate to take into account two different situations: |