1 /* 2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @modules java.base/jdk.internal.misc 27 * @key stress gc 28 * 29 * @summary converted from VM Testbase gc/memory/Nio. 30 * VM Testbase keywords: [gc, stress, stressopt, monitoring] 31 * 32 * @library /vmTestbase 33 * /test/lib 34 * @run driver jdk.test.lib.FileInstaller . . 35 * @run main/othervm -XX:MaxDirectMemorySize=50M gc.memory.Nio.Nio 36 */ 37 38 package gc.memory.Nio; 39 40 import java.lang.management.ManagementFactory; 41 import java.nio.ByteBuffer; 42 import jdk.internal.misc.VM; 43 import com.sun.management.HotSpotDiagnosticMXBean; 44 import java.io.File; 45 import java.io.IOException; 46 47 /** 48 * Test that uses java.nio.ByteBuffer to allocate native memory. 49 * The test allocates all the memory available and checks that 50 * further attempts to allocate more more will fail. 51 * Test also checks that allocating native memory doesn't affect heap. 52 * Test also cheks that GC can find unused native memory. 53 * 54 * @summary Checks that nio.ByteBuffer allocates native memory and doesn't affect heap. 55 * @run main/othervm -XX:MaxDirectMemorySize=50M gc.memory.Nio.Nio 56 */ 57 public class Nio { 58 59 static final int MAX_SIZE = (int)VM.maxDirectMemory(); 60 61 public static void main(String[] args) { 62 System.exit(new Nio().run() + 95 /*STATUS_BASE*/); 63 } 64 65 public Nio() { 66 } 67 68 public int run() { 69 // Step0: init 70 gc(); 71 long usedHeap_0 = getUsedHeap(); 72 long usedNonHeap_0 = getUsedNonHeap(); 73 74 // Step1: allocate the all available direct memory 75 // no OOME, no heap memory should be used 76 System.out.println("Allocating all the direct memory: " + MAX_SIZE); 77 ByteBuffer bb; 78 try { 79 bb = ByteBuffer.allocateDirect((int)MAX_SIZE); 80 System.out.println("... success"); 81 } catch (OutOfMemoryError oom) { 82 throw new Fault("Unexpected OOME during the first allocation " + oom); 83 } 84 long usedHeap_1 = getUsedHeap(); 85 long usedNonHeap_1 = getUsedNonHeap(); 86 checkHeapIsNotAffected(usedHeap_0, usedHeap_1, usedNonHeap_0, usedNonHeap_1); 87 // Step2: invoke GC, it shouldn't help. 88 System.out.println("Doing gc"); 89 gc(); 90 91 // Step3: allocate 1 byte in the direct memory 92 // OOM is expected 93 try { 94 System.out.println("Allocating 1 byte"); 95 ByteBuffer.allocateDirect(1); 96 throw new Fault("No OOM, but we already allocated all the memory"); 97 } catch (OutOfMemoryError oom) { 98 System.out.println("Expected OOM " + oom); 99 } 100 101 // Step4: read and write into allocated memory 102 double d0 = -3.1415; 103 float f0 = 41234.6f; 104 bb.putDouble(MAX_SIZE/2, d0); 105 bb.putFloat(MAX_SIZE - 17, f0); 106 double d1 = bb.getDouble(MAX_SIZE/2); 107 float f1 = bb.getFloat(MAX_SIZE - 17); 108 System.out.println("put: " + d0 + ", " + f0); 109 System.out.println("got: " + d1 + ", " + f1); 110 if (d0 != d1 || f0 != f1) { 111 throw new Fault("read/write to buffer check failed"); 112 } 113 114 // Step5: 115 // clean the buffer, use gc, try to allocate again 116 // no OOM is expected 117 bb = null; 118 gc(); 119 try { 120 System.out.println("Allocating 10 bytes"); 121 ByteBuffer.allocateDirect(10); 122 } catch (OutOfMemoryError oom) { 123 throw new Fault("Nop, OOM is unexpected again: " + oom); 124 } 125 126 127 System.out.println("The long quest has done! Congratulations"); 128 129 return 0; 130 } 131 132 133 public static void gc() { 134 System.gc(); 135 try { 136 Thread.currentThread().sleep(200); 137 } catch (Exception ignore) { 138 } 139 } 140 141 /** 142 * @return the size of used heap 143 */ 144 public static long getUsedHeap() { 145 return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); 146 } 147 148 /** 149 * @return the size of used non heap 150 */ 151 public static long getUsedNonHeap() { 152 return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed(); 153 } 154 155 /** 156 * Check that heap and non-heap memory have NOT changed significantly. 157 * Throws a Fault if check failed. 158 * 159 * @param h_before used heap before 160 * @param h_after used heap after 161 * @param nh_before used non heap before 162 * @param nh_after used non heap after 163 */ 164 void checkHeapIsNotAffected(long h_before, long h_after, long nh_before, long nh_after) { 165 166 if (h_after - h_before > MAX_SIZE * 0.75) { 167 System.err.println("Used heap before: " + h_before); 168 System.err.println("Used heap after : " + h_after); 169 dumpHeap(); 170 String failed = "Allocating direct memory should not eat the heap!" 171 + " Heap dumped to heapDump.hprof file."; 172 throw new Fault(failed); 173 } 174 if (nh_after - nh_before > MAX_SIZE * 0.75) { 175 System.err.println("Used heap before: " + nh_before); 176 System.err.println("Used heap after : " + nh_after); 177 dumpHeap(); 178 throw new Fault("Allocating direct memory should not eat non the heap!"); 179 } 180 } 181 182 /** 183 * Try to make heap dump 184 */ 185 void dumpHeap() { 186 HotSpotDiagnosticMXBean mxBean = ManagementFactory 187 .getPlatformMXBean(HotSpotDiagnosticMXBean.class); 188 try { 189 System.out.println("Try to dump heap to heapDump.hprof file.."); 190 mxBean.dumpHeap("heapDump.hprof", false); 191 System.out.println("Done"); 192 } catch (IOException e) { 193 System.out.println("Failed to dump heap"); 194 e.printStackTrace(); 195 } 196 } 197 198 /** 199 * RuntimeException signaling a test failure. 200 */ 201 public static class Fault extends RuntimeException { 202 public Fault(String message) { 203 super(message); 204 } 205 } 206 207 }