// Test that calling registerNativeAllocation triggers a GC eventually // after a substantial number of registered native bytes. privatestaticvoid checkRegisterNativeAllocation() throws Exception { long maxMem = Runtime.getRuntime().maxMemory(); int size = (int)(maxMem / 32); int allocationCount = 256; finallong startTime = System.currentTimeMillis();
int initialFinalizeCount = finalizeCounter.get();
ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
ref = $noinline$allocPhantom(queue); long total = 0; int i; for (i = 0; !ref.isEnqueued() && i < allocationCount; ++i) {
runtime.registerNativeAllocation(size);
total += size;
// Allocate a new finalizable object each time, so that we can see if anything // was finalized while we were running.
$noinline$allocateIncrementingFinalizer();
// Sleep a little bit to ensure not all of the calls to // registerNativeAllocation complete while GC is in the process of // running. Thread.sleep(MAX_EXPECTED_GC_DURATION_MS / allocationCount);
}
// Wait up to MAX_EXPECTED_GC_DURATION_MS to give GC a chance to finish // running. If the reference isn't enqueued after that, then it is // pretty unlikely (though technically still possible) that GC was // triggered as intended. if (queue.remove(MAX_EXPECTED_GC_DURATION_MS) == null) {
System.out.println("GC failed to complete after " + i
+ " iterations, is_enqueued = " + ref.isEnqueued());
System.out.println(" size = " + size + ", elapsed msecs = "
+ (System.currentTimeMillis() - startTime));
System.out.println(" original maxMemory() = " + maxMem + " current maxMemory() = "
+ Runtime.getRuntime().maxMemory());
System.out.println(" Initial finalize count = " + initialFinalizeCount
+ " current finalize count = " + finalizeCounter.get()); Thread.sleep(2 * MAX_EXPECTED_GC_DURATION_MS);
System.out.println(" After delay, queue.poll() = " + queue.poll()
+ " is_enqueued = " + ref.isEnqueued());
System.out.println(" elapsed msecs = " + (System.currentTimeMillis() - startTime));
Runtime.getRuntime().gc();
System.runFinalization();
System.out.println(" After GC, queue.poll() = " + queue.poll()
+ " is_enqueued = " + ref.isEnqueued());
}
while (total > 0) {
runtime.registerNativeFree(size);
total -= size;
}
}
// Call registerNativeAllocation repeatedly at a high rate to trigger the case of blocking // registerNativeAllocation. Stop before we risk exhausting the finalizer timeout. privatestaticvoid triggerBlockingRegisterNativeAllocation() throws Exception { finallong startTime = System.currentTimeMillis(); finallong finalizerTimeoutMs = VMRuntime.getRuntime().getFinalizerTimeoutMs(); finallong quittingTime = startTime + finalizerTimeoutMs - MAX_EXPECTED_GC_DURATION_MS; long maxMem = Runtime.getRuntime().maxMemory(); int size = (int)(maxMem / 5); int allocationCount = 10;
long total = 0; for (int i = 0; i < allocationCount && System.currentTimeMillis() < quittingTime; ++i) {
runtime.registerNativeAllocation(size);
total += size;
}
while (total > 0) {
runtime.registerNativeFree(size);
total -= size;
}
}
publicstaticvoid main(String[] args) throws Exception { // Test that registerNativeAllocation triggers GC. // Run this a few times in a loop to reduce the chances that the test // is flaky and make sure registerNativeAllocation continues to work // after the first GC is triggered. for (int i = 0; i < 20; ++i) {
checkRegisterNativeAllocation();
}
// Test that we don't get a deadlock if we call // registerNativeAllocation with a blocked finalizer. synchronized (deadlockLock) {
$noinline$allocateDeadlockingFinalizer(); while (!aboutToDeadlock) {
Runtime.getRuntime().gc();
}
// Do more allocations now that the finalizer thread is deadlocked so that we force // finalization and timeout.
triggerBlockingRegisterNativeAllocation();
}
System.out.println("Test complete");
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 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.