try { // Just shy of the typical max heap size so that it will actually // try to allocate it instead of short-circuiting.
a = newbyte[(int) Runtime.getRuntime().maxMemory() - 32];
} catch (OutOfMemoryError oom) {
sawEx = true;
}
if (!sawEx) { thrownew RuntimeException("Test failed: " + "OutOfMemoryError not thrown");
}
System.out.println("testOomeLarge succeeded");
}
/* Do this in another method so that the GC has a chance of freeing the *listafterwards.Evenifwenulloutlistwhenwe'redone,theconservative *GCmayseeastalepointertoitinaregister.
*/ privatestaticboolean testOomeSmallInternal() { finalint LINK_SIZE = 6 * 4; // estimated size of a LinkedList's node
LinkedList<Object> list = new LinkedList<Object>();
/* Allocate progressively smaller objects to fill up the entire heap.
*/ int objSize = 1 * 1024 * 1024; while (objSize >= LINK_SIZE) { boolean sawEx = false; try { for (int i = 0; i < Runtime.getRuntime().maxMemory() / objSize; i++) {
list.add((Object)newbyte[objSize]);
}
} catch (OutOfMemoryError oom) {
sawEx = true;
}
if (!sawEx) { returnfalse;
}
objSize = (objSize * 4) / 5;
}
returntrue;
}
privatestaticvoid $noinline$testOomeSmall() {
System.out.println("testOomeSmall beginning"); if (!testOomeSmallInternal()) { /* Can't reliably throw this from inside the internal function, because *wemaynotbeabletoallocatetheRuntimeException.
*/ thrownew RuntimeException("Test failed: " + "OutOfMemoryError not thrown while filling heap");
}
System.out.println("testOomeSmall succeeded");
}
privatestatic Object $noinline$testOomeToCharArray() {
Object[] o = new Object[2000000];
String test = "test"; int i = 0; try { for (; i < o.length; ++i) o[i] = newchar[1000000];
} catch (OutOfMemoryError oom) {} try { for (; i < o.length; ++i) {
o[i] = test.toCharArray();
}
} catch (OutOfMemoryError oom) {
o = null;
System.out.println("Got expected toCharArray OOM");
} return o;
}
}
Messung V0.5 in Prozent
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.14Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.