Path: blob/master/test/jdk/java/nio/Buffer/LimitDirectMemory.java
41149 views
/*1* Copyright (c) 2002, 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* @bug 4627316 674352626* @summary Test option to limit direct memory allocation27* @requires (os.arch == "x86_64") | (os.arch == "amd64")28* @library /test/lib29*30* @summary Test: memory is properly limited using multiple buffers31* @run main/othervm -XX:MaxDirectMemorySize=10 LimitDirectMemory true 10 132* @run main/othervm -XX:MaxDirectMemorySize=1k LimitDirectMemory true 1k 10033* @run main/othervm -XX:MaxDirectMemorySize=10m LimitDirectMemory true 10m 10m34*35* @summary Test: We can increase the amount of available memory36* @run main/othervm -XX:MaxDirectMemorySize=65M LimitDirectMemory false 64M 65M37*38* @summary Test: Exactly the default amount of memory is available39* @run main/othervm LimitDirectMemory false 10 140* @run main/othervm -Xmx64m LimitDirectMemory false 0 DEFAULT41* @run main/othervm -Xmx64m LimitDirectMemory true 0 DEFAULT+142*43* @summary Test: We should be able to eliminate direct memory allocation entirely44* @run main/othervm -XX:MaxDirectMemorySize=0 LimitDirectMemory true 0 145*46* @summary Test: Setting the system property should not work so we should be able47* to allocate the default amount48* @run main/othervm -Dsun.nio.MaxDirectMemorySize=1K -Xmx64m49* LimitDirectMemory false DEFAULT-1 DEFAULT/250*/5152import java.nio.ByteBuffer;53import java.util.Properties;5455public class LimitDirectMemory {56private static final int K = 1024;5758public static void main(String [] args) throws Exception {59if (args.length < 2) {60throw new IllegalArgumentException("Usage: "61+ "java LimitDirectMemory"62+ " <OOME_expected(true|false)>"63+ " <fill_direct_memory>"64+ " <size_per_buffer>");65}66boolean throwp = parseThrow(args[0]);67int size = parseSize(args[1]);68int incr = (args.length > 2 ? parseSize(args[2]) : size);6970Properties p = System.getProperties();71if (p.getProperty("sun.nio.MaxDirectMemorySize") != null)72throw new RuntimeException("sun.nio.MaxDirectMemorySize defined");7374ByteBuffer [] b = new ByteBuffer[K];7576// Fill up most/all of the direct memory77int i = 0;78while (size >= incr) {79b[i++] = ByteBuffer.allocateDirect(incr);80size -= incr;81}8283if (throwp) {84try {85b[i] = ByteBuffer.allocateDirect(incr);86throw new RuntimeException("OutOfMemoryError not thrown: "87+ incr);88} catch (OutOfMemoryError e) {89e.printStackTrace(System.out);90System.out.println("OK - Error thrown as expected ");91}92} else {93b[i] = ByteBuffer.allocateDirect(incr);94System.out.println("OK - Error not thrown");95}96}9798private static boolean parseThrow(String s) {99if (s.equals("true")) return true;100if (s.equals("false")) return false;101throw new RuntimeException("Unrecognized expectation: " + s);102}103104private static int parseSize(String size) throws Exception {105106if (size.equals("DEFAULT"))107return (int)Runtime.getRuntime().maxMemory();108if (size.equals("DEFAULT+1"))109return (int)Runtime.getRuntime().maxMemory() + 1;110if (size.equals("DEFAULT+1M"))111return (int)Runtime.getRuntime().maxMemory() + (1 << 20);112if (size.equals("DEFAULT-1"))113return (int)Runtime.getRuntime().maxMemory() - 1;114if (size.equals("DEFAULT/2"))115return (int)Runtime.getRuntime().maxMemory() / 2;116117int idx = 0, len = size.length();118119120for (int i = 0; i < len; i++) {121if (Character.isDigit(size.charAt(i))) idx++;122else break;123}124125if (idx == 0)126throw new RuntimeException("No digits detected: " + size);127128int result = Integer.parseInt(size.substring(0, idx));129130if (idx < len) {131for (int i = idx; i < len; i++) {132switch(size.charAt(i)) {133case 'T': case 't': result *= K; // fall through134case 'G': case 'g': result *= K; // fall through135case 'M': case 'm': result *= K; // fall through136case 'K': case 'k': result *= K;137break;138default:139throw new RuntimeException("Unrecognized size: " + size);140}141}142}143return result;144}145}146147148