privatestaticint exhaustJavaHeap(Object[] data, int index, int size) {
Runtime.getRuntime().gc(); while (index != data.length) { try {
data[index] = newbyte[size];
++index;
} catch (OutOfMemoryError oome) { // Rapidly shrink the object size to fill any remaining space. // Use few different sizes, since detecting out-of-memory is slow. if (size >= 32) {
size /= 32;
} elseif (size > 1) {
size = 1;
} else { break;
}
}
} return index;
}
publicstatic Object eatAllMemory() {
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;
}
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.