/* * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Test that uses java.nio.ByteBuffer to allocate native memory. * The test allocates all the memory available and checks that * further attempts to allocate more more will fail. * Test also checks that allocating native memory doesn't affect heap. * Test also cheks that GC can find unused native memory. * * @summary Checks that nio.ByteBuffer allocates native memory and doesn't affect heap. * @run main/othervm -XX:MaxDirectMemorySize=50M gc.memory.Nio.Nio
*/ publicclass Nio {
publicint run() { // Step0: init
gc(); long usedHeap_0 = getUsedHeap(); long usedNonHeap_0 = getUsedNonHeap();
// Step1: allocate the all available direct memory // no OOME, no heap memory should be used
System.out.println("Allocating all the direct memory: " + MAX_SIZE);
ByteBuffer bb; try {
bb = ByteBuffer.allocateDirect((int)MAX_SIZE);
System.out.println("... success");
} catch (OutOfMemoryError oom) { thrownew Fault("Unexpected OOME during the first allocation " + oom);
} long usedHeap_1 = getUsedHeap(); long usedNonHeap_1 = getUsedNonHeap();
checkHeapIsNotAffected(usedHeap_0, usedHeap_1, usedNonHeap_0, usedNonHeap_1); // Step2: invoke GC, it shouldn't help.
System.out.println("Doing gc");
gc();
// Step3: allocate 1 byte in the direct memory // OOM is expected try {
System.out.println("Allocating 1 byte");
ByteBuffer.allocateDirect(1); thrownew Fault("No OOM, but we already allocated all the memory");
} catch (OutOfMemoryError oom) {
System.out.println("Expected OOM " + oom);
}
/** * @return the size of used heap
*/ publicstaticlong getUsedHeap() { return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
}
/** * @return the size of used non heap
*/ publicstaticlong getUsedNonHeap() { return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed();
}
/** * Check that heap and non-heap memory have NOT changed significantly. * Throws a Fault if check failed. * * @param h_before used heap before * @param h_after used heap after * @param nh_before used non heap before * @param nh_after used non heap after
*/ void checkHeapIsNotAffected(long h_before, long h_after, long nh_before, long nh_after) {
if (h_after - h_before > MAX_SIZE * 0.75) {
System.err.println("Used heap before: " + h_before);
System.err.println("Used heap after : " + h_after);
dumpHeap();
String failed = "Allocating direct memory should not eat the heap!"
+ " Heap dumped to heapDump.hprof file."; thrownew Fault(failed);
} if (nh_after - nh_before > MAX_SIZE * 0.75) {
System.err.println("Used heap before: " + nh_before);
System.err.println("Used heap after : " + nh_after);
dumpHeap(); thrownew Fault("Allocating direct memory should not eat non the heap!");
}
}
/** * Try to make heap dump
*/ void dumpHeap() {
HotSpotDiagnosticMXBean mxBean = ManagementFactory
.getPlatformMXBean(HotSpotDiagnosticMXBean.class); try {
System.out.println("Try to dump heap to heapDump.hprof file..");
mxBean.dumpHeap("heapDump.hprof", false);
System.out.println("Done");
} catch (IOException e) {
System.out.println("Failed to dump heap");
e.printStackTrace();
}
}
/** * RuntimeException signaling a test failure.
*/ publicstaticclass Fault extends RuntimeException { public Fault(String message) { super(message);
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
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.