private static void testOomeLarge() {
System.out.println("testOomeLarge beginning");
- /* Just shy of the typical max heap size so that it will actually
- * try to allocate it instead of short-circuiting.
- *
- * TODO: stop assuming the VM defaults to 16MB max
- */
- final int SIXTEEN_MB = (16 * 1024 * 1024 - 32);
-
Boolean sawEx = false;
- byte a[];
+ byte[] a;
try {
- a = new byte[SIXTEEN_MB];
+ // Just shy of the typical max heap size so that it will actually
+ // try to allocate it instead of short-circuiting.
+ a = new byte[(int) Runtime.getRuntime().maxMemory() - 32];
} catch (OutOfMemoryError oom) {
//Log.i(TAG, "HeapTest/OomeLarge caught " + oom);
sawEx = true;
/* Do this in another method so that the GC has a chance of freeing the
* list afterwards. Even if we null out list when we're done, the conservative
* GC may see a stale pointer to it in a register.
- *
- * TODO: stop assuming the VM defaults to 16MB max
*/
private static boolean testOomeSmallInternal() {
- final int SIXTEEN_MB = (16 * 1024 * 1024);
final int LINK_SIZE = 6 * 4; // estimated size of a LinkedList's node
LinkedList<Object> list = new LinkedList<Object>();
while (objSize >= LINK_SIZE) {
boolean sawEx = false;
try {
- for (int i = 0; i < SIXTEEN_MB / objSize; i++) {
+ for (int i = 0; i < Runtime.getRuntime().maxMemory() / objSize; i++) {
list.add((Object)new byte[objSize]);
}
} catch (OutOfMemoryError oom) {