Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/memory/Nio/Nio.java
41159 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @modules java.base/jdk.internal.misc26* @key stress27*28* @summary converted from VM Testbase gc/memory/Nio.29* VM Testbase keywords: [gc, stress, stressopt, monitoring]30*31* @library /vmTestbase32* /test/lib33* @run main/othervm -XX:MaxDirectMemorySize=50M gc.memory.Nio.Nio34*/3536package gc.memory.Nio;3738import java.lang.management.ManagementFactory;39import java.nio.ByteBuffer;40import jdk.internal.misc.VM;41import com.sun.management.HotSpotDiagnosticMXBean;42import java.io.File;43import java.io.IOException;4445/**46* Test that uses java.nio.ByteBuffer to allocate native memory.47* The test allocates all the memory available and checks that48* further attempts to allocate more more will fail.49* Test also checks that allocating native memory doesn't affect heap.50* Test also cheks that GC can find unused native memory.51*52* @summary Checks that nio.ByteBuffer allocates native memory and doesn't affect heap.53* @run main/othervm -XX:MaxDirectMemorySize=50M gc.memory.Nio.Nio54*/55public class Nio {5657static final int MAX_SIZE = (int)VM.maxDirectMemory();5859public static void main(String[] args) {60System.exit(new Nio().run() + 95 /*STATUS_BASE*/);61}6263public Nio() {64}6566public int run() {67// Step0: init68gc();69long usedHeap_0 = getUsedHeap();70long usedNonHeap_0 = getUsedNonHeap();7172// Step1: allocate the all available direct memory73// no OOME, no heap memory should be used74System.out.println("Allocating all the direct memory: " + MAX_SIZE);75ByteBuffer bb;76try {77bb = ByteBuffer.allocateDirect((int)MAX_SIZE);78System.out.println("... success");79} catch (OutOfMemoryError oom) {80throw new Fault("Unexpected OOME during the first allocation " + oom);81}82long usedHeap_1 = getUsedHeap();83long usedNonHeap_1 = getUsedNonHeap();84checkHeapIsNotAffected(usedHeap_0, usedHeap_1, usedNonHeap_0, usedNonHeap_1);85// Step2: invoke GC, it shouldn't help.86System.out.println("Doing gc");87gc();8889// Step3: allocate 1 byte in the direct memory90// OOM is expected91try {92System.out.println("Allocating 1 byte");93ByteBuffer.allocateDirect(1);94throw new Fault("No OOM, but we already allocated all the memory");95} catch (OutOfMemoryError oom) {96System.out.println("Expected OOM " + oom);97}9899// Step4: read and write into allocated memory100double d0 = -3.1415;101float f0 = 41234.6f;102bb.putDouble(MAX_SIZE/2, d0);103bb.putFloat(MAX_SIZE - 17, f0);104double d1 = bb.getDouble(MAX_SIZE/2);105float f1 = bb.getFloat(MAX_SIZE - 17);106System.out.println("put: " + d0 + ", " + f0);107System.out.println("got: " + d1 + ", " + f1);108if (d0 != d1 || f0 != f1) {109throw new Fault("read/write to buffer check failed");110}111112// Step5:113// clean the buffer, use gc, try to allocate again114// no OOM is expected115bb = null;116gc();117try {118System.out.println("Allocating 10 bytes");119ByteBuffer.allocateDirect(10);120} catch (OutOfMemoryError oom) {121throw new Fault("Nop, OOM is unexpected again: " + oom);122}123124125System.out.println("The long quest has done! Congratulations");126127return 0;128}129130131public static void gc() {132System.gc();133try {134Thread.currentThread().sleep(200);135} catch (Exception ignore) {136}137}138139/**140* @return the size of used heap141*/142public static long getUsedHeap() {143return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();144}145146/**147* @return the size of used non heap148*/149public static long getUsedNonHeap() {150return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed();151}152153/**154* Check that heap and non-heap memory have NOT changed significantly.155* Throws a Fault if check failed.156*157* @param h_before used heap before158* @param h_after used heap after159* @param nh_before used non heap before160* @param nh_after used non heap after161*/162void checkHeapIsNotAffected(long h_before, long h_after, long nh_before, long nh_after) {163164if (h_after - h_before > MAX_SIZE * 0.75) {165System.err.println("Used heap before: " + h_before);166System.err.println("Used heap after : " + h_after);167dumpHeap();168String failed = "Allocating direct memory should not eat the heap!"169+ " Heap dumped to heapDump.hprof file.";170throw new Fault(failed);171}172if (nh_after - nh_before > MAX_SIZE * 0.75) {173System.err.println("Used heap before: " + nh_before);174System.err.println("Used heap after : " + nh_after);175dumpHeap();176throw new Fault("Allocating direct memory should not eat non the heap!");177}178}179180/**181* Try to make heap dump182*/183void dumpHeap() {184HotSpotDiagnosticMXBean mxBean = ManagementFactory185.getPlatformMXBean(HotSpotDiagnosticMXBean.class);186try {187System.out.println("Try to dump heap to heapDump.hprof file..");188mxBean.dumpHeap("heapDump.hprof", false);189System.out.println("Done");190} catch (IOException e) {191System.out.println("Failed to dump heap");192e.printStackTrace();193}194}195196/**197* RuntimeException signaling a test failure.198*/199public static class Fault extends RuntimeException {200public Fault(String message) {201super(message);202}203}204205}206207208