summaryrefslogtreecommitdiff
path: root/test/159-app-image-fields/src/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'test/159-app-image-fields/src/Main.java')
-rw-r--r--test/159-app-image-fields/src/Main.java59
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: