0N/A/*
553N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.util.Properties;
0N/A
0N/Apublic class LimitDirectMemory {
0N/A private static int K = 1024;
0N/A
288N/A public static void main(String [] args) throws Exception {
0N/A if (args.length < 2)
0N/A throw new RuntimeException();
0N/A boolean throwp = parseThrow(args[0]);
0N/A int size = parseSize(args[1]);
0N/A int incr = (args.length > 2 ? parseSize(args[2]) : size);
0N/A
0N/A Properties p = System.getProperties();
0N/A if (p.getProperty("sun.nio.MaxDirectMemorySize") != null)
0N/A throw new RuntimeException("sun.nio.MaxDirectMemorySize defined");
0N/A
0N/A ByteBuffer [] b = new ByteBuffer[K];
0N/A
0N/A // Fill up most/all of the direct memory
0N/A int i = 0;
0N/A while (size >= incr) {
0N/A b[i++] = ByteBuffer.allocateDirect(incr);
0N/A size -= incr;
0N/A }
0N/A
if (throwp) {
try {
b[i] = ByteBuffer.allocateDirect(incr);
throw new RuntimeException("OutOfMemoryError not thrown: "
+ incr);
} catch (OutOfMemoryError e) {
e.printStackTrace(System.out);
System.out.println("OK - Error thrown as expected ");
}
} else {
b[i] = ByteBuffer.allocateDirect(incr);
System.out.println("OK - Error not thrown");
}
}
private static boolean parseThrow(String s) {
if (s.equals("true")) return true;
if (s.equals("false")) return false;
throw new RuntimeException("Unrecognized expectation: " + s);
}
private static int parseSize(String size) throws Exception {
if (size.equals("DEFAULT"))
return (int)Runtime.getRuntime().maxMemory();
if (size.equals("DEFAULT+1"))
return (int)Runtime.getRuntime().maxMemory() + 1;
if (size.equals("DEFAULT+1M"))
return (int)Runtime.getRuntime().maxMemory() + (1 << 20);
if (size.equals("DEFAULT-1"))
return (int)Runtime.getRuntime().maxMemory() - 1;
if (size.equals("DEFAULT/2"))
return (int)Runtime.getRuntime().maxMemory() / 2;
int idx = 0, len = size.length();
int result = 1;
for (int i = 0; i < len; i++) {
if (Character.isDigit(size.charAt(i))) idx++;
else break;
}
if (idx == 0)
throw new RuntimeException("No digits detected: " + size);
result = Integer.parseInt(size.substring(0, idx));
if (idx < len) {
for (int i = idx; i < len; i++) {
switch(size.charAt(i)) {
case 'T': case 't': result *= K; // fall through
case 'G': case 'g': result *= K; // fall through
case 'M': case 'm': result *= K; // fall through
case 'K': case 'k': result *= K;
break;
default:
throw new RuntimeException("Unrecognized size: " + size);
}
}
}
return result;
}
}