// Repeatedly inform the GC of native allocations. Return the time (in nsecs) this takes. privatestaticlong timeNotifications() { final VMRuntime vmr = VMRuntime.getRuntime(); finallong startNanos = System.nanoTime(); // Iteration count must be >= Heap::kNotifyNativeInterval. for (int i = 0; i < 400; ++i) {
vmr.notifyNativeAllocation();
} return System.nanoTime() - startNanos;
}
publicstaticvoid main(String[] args) {
System.loadLibrary(args[0]);
System.out.println("Main Started"); for (int i = 1; i <= MAX_TRIES; ++i) {
Runtime.getRuntime().gc(); if (new Main().tryToRun(i == MAX_TRIES)) { break;
} if (i == MAX_TRIES / 2) { // Maybe some transient CPU load is causing issues here? try { Thread.sleep(3000);
} catch (InterruptedException ignored) {
System.out.println("Unexpected interrupt");
}
} // Clean up and try again.
Runtime.getRuntime().gc();
System.runFinalization();
}
System.out.println("Main Finished");
}
// Returns false on a failure that should be retried. boolean tryToRun(boolean lastChance) { finalint startingGcNum = getGcNum();
timeNotifications(); // warm up. finallong referenceTime1 = timeNotifications(); finallong referenceTime2 = timeNotifications(); finallong referenceTime3 = timeNotifications(); finallong referenceTime = Math.min(referenceTime1, Math.min(referenceTime2, referenceTime3));
// Allocate a GB+ of native memory without informing the GC. for (int i = 0; i < HOW_MANY_HUGE; ++i) { new BufferHolder();
}
if (startingGcNum != getGcNum()) { // Happens rarely, fail and retry. if (!lastChance) { returnfalse;
}
System.out.println("Triggered early GC");
} // One of the notifications should block for GC to catch up. long actualTime = timeNotifications(); finallong minBlockingTime = 2 * referenceTime + 2_000_000;
if (startingGcNum == getGcNum()) {
System.out.println("No gc completed");
} if (actualTime > 500_000_000 * (isVm() ? 4 : 1)) {
System.out.println("Notifications ran too slowly; excessive blocking? msec = "
+ (actualTime / 1_000_000));
} elseif (actualTime < minBlockingTime) { if (!lastChance) { // We sometimes see this, maybe because a GC is triggered by other means? // Try again before reporting. returnfalse;
}
System.out.println("Notifications ran too quickly; no blocking GC? msec = "
+ (actualTime / 1_000_000) + " reference(msec) = " + (referenceTime / 1_000_000));
}
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.