Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/Buffer/LimitDirectMemory.java
38813 views
/*1* Copyright (c) 2002, 2010, 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*/2223import java.nio.ByteBuffer;24import java.util.Properties;2526public class LimitDirectMemory {27private static int K = 1024;2829public static void main(String [] args) throws Exception {30if (args.length < 2)31throw new RuntimeException();32boolean throwp = parseThrow(args[0]);33int size = parseSize(args[1]);34int incr = (args.length > 2 ? parseSize(args[2]) : size);3536Properties p = System.getProperties();37if (p.getProperty("sun.nio.MaxDirectMemorySize") != null)38throw new RuntimeException("sun.nio.MaxDirectMemorySize defined");3940ByteBuffer [] b = new ByteBuffer[K];4142// Fill up most/all of the direct memory43int i = 0;44while (size >= incr) {45b[i++] = ByteBuffer.allocateDirect(incr);46size -= incr;47}4849if (throwp) {50try {51b[i] = ByteBuffer.allocateDirect(incr);52throw new RuntimeException("OutOfMemoryError not thrown: "53+ incr);54} catch (OutOfMemoryError e) {55e.printStackTrace(System.out);56System.out.println("OK - Error thrown as expected ");57}58} else {59b[i] = ByteBuffer.allocateDirect(incr);60System.out.println("OK - Error not thrown");61}62}6364private static boolean parseThrow(String s) {65if (s.equals("true")) return true;66if (s.equals("false")) return false;67throw new RuntimeException("Unrecognized expectation: " + s);68}6970private static int parseSize(String size) throws Exception {7172if (size.equals("DEFAULT"))73return (int)Runtime.getRuntime().maxMemory();74if (size.equals("DEFAULT+1"))75return (int)Runtime.getRuntime().maxMemory() + 1;76if (size.equals("DEFAULT+1M"))77return (int)Runtime.getRuntime().maxMemory() + (1 << 20);78if (size.equals("DEFAULT-1"))79return (int)Runtime.getRuntime().maxMemory() - 1;80if (size.equals("DEFAULT/2"))81return (int)Runtime.getRuntime().maxMemory() / 2;8283int idx = 0, len = size.length();8485int result = 1;86for (int i = 0; i < len; i++) {87if (Character.isDigit(size.charAt(i))) idx++;88else break;89}9091if (idx == 0)92throw new RuntimeException("No digits detected: " + size);9394result = Integer.parseInt(size.substring(0, idx));9596if (idx < len) {97for (int i = idx; i < len; i++) {98switch(size.charAt(i)) {99case 'T': case 't': result *= K; // fall through100case 'G': case 'g': result *= K; // fall through101case 'M': case 'm': result *= K; // fall through102case 'K': case 'k': result *= K;103break;104default:105throw new RuntimeException("Unrecognized size: " + size);106}107}108}109return result;110}111}112113114