corda/sgx-jvm/avian/test/OutOfMemory.java
Andras Slemmer 9bb3d6b972 Add 'sgx-jvm/avian/' from commit '09e4fe60d01f4f4bfb6b2976973bb4913ef61edc'
git-subtree-dir: sgx-jvm/avian
git-subtree-mainline: f978eab8d1
git-subtree-split: 09e4fe60d0
2017-03-13 12:18:24 +00:00

63 lines
1.2 KiB
Java

public class OutOfMemory {
// assume a 128MB heap size:
private static final int Padding = 120 * 1024 * 1024;
private static class Node {
Object value;
Node next;
}
private static void bigObjects() {
Object[] root = null;
while (true) {
Object[] x = new Object[1024 * 1024];
x[0] = root;
root = x;
}
}
private static void littleObjects() {
byte[] padding = new byte[Padding];
Node root = null;
while (true) {
Node x = new Node();
x.next = root;
root = x;
}
}
private static void bigAndLittleObjects() {
byte[] padding = new byte[Padding];
Node root = null;
while (true) {
Node x = new Node();
x.value = new Object[1024 * 1024];
x.next = root;
root = x;
}
}
public static void main(String[] args) {
try {
bigObjects();
throw new RuntimeException();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
try {
littleObjects();
throw new RuntimeException();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
try {
bigAndLittleObjects();
throw new RuntimeException();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
}