author | alanb |
Fri, 15 Oct 2010 15:09:37 +0100 | |
changeset 6897 | 561a431cf238 |
child 37913 | 3cc95b690353 |
permissions | -rw-r--r-- |
6897
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
1 |
/* |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
4 |
* |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
8 |
* |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
14 |
* |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
18 |
* |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
21 |
* questions. |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
22 |
*/ |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
23 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
24 |
import java.nio.ByteBuffer; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
25 |
import java.util.Properties; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
26 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
27 |
public class LimitDirectMemory { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
28 |
private static int K = 1024; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
29 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
30 |
public static void main(String [] args) throws Exception { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
31 |
if (args.length < 2) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
32 |
throw new RuntimeException(); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
33 |
boolean throwp = parseThrow(args[0]); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
34 |
int size = parseSize(args[1]); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
35 |
int incr = (args.length > 2 ? parseSize(args[2]) : size); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
36 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
37 |
Properties p = System.getProperties(); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
38 |
if (p.getProperty("sun.nio.MaxDirectMemorySize") != null) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
39 |
throw new RuntimeException("sun.nio.MaxDirectMemorySize defined"); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
40 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
41 |
ByteBuffer [] b = new ByteBuffer[K]; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
42 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
43 |
// Fill up most/all of the direct memory |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
44 |
int i = 0; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
45 |
while (size >= incr) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
46 |
b[i++] = ByteBuffer.allocateDirect(incr); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
47 |
size -= incr; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
48 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
49 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
50 |
if (throwp) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
51 |
try { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
52 |
b[i] = ByteBuffer.allocateDirect(incr); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
53 |
throw new RuntimeException("OutOfMemoryError not thrown: " |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
54 |
+ incr); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
55 |
} catch (OutOfMemoryError e) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
56 |
e.printStackTrace(System.out); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
57 |
System.out.println("OK - Error thrown as expected "); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
58 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
59 |
} else { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
60 |
b[i] = ByteBuffer.allocateDirect(incr); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
61 |
System.out.println("OK - Error not thrown"); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
62 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
63 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
64 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
65 |
private static boolean parseThrow(String s) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
66 |
if (s.equals("true")) return true; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
67 |
if (s.equals("false")) return false; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
68 |
throw new RuntimeException("Unrecognized expectation: " + s); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
69 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
70 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
71 |
private static int parseSize(String size) throws Exception { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
72 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
73 |
if (size.equals("DEFAULT")) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
74 |
return (int)Runtime.getRuntime().maxMemory(); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
75 |
if (size.equals("DEFAULT+1")) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
76 |
return (int)Runtime.getRuntime().maxMemory() + 1; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
77 |
if (size.equals("DEFAULT+1M")) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
78 |
return (int)Runtime.getRuntime().maxMemory() + (1 << 20); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
79 |
if (size.equals("DEFAULT-1")) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
80 |
return (int)Runtime.getRuntime().maxMemory() - 1; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
81 |
if (size.equals("DEFAULT/2")) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
82 |
return (int)Runtime.getRuntime().maxMemory() / 2; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
83 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
84 |
int idx = 0, len = size.length(); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
85 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
86 |
int result = 1; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
87 |
for (int i = 0; i < len; i++) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
88 |
if (Character.isDigit(size.charAt(i))) idx++; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
89 |
else break; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
90 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
91 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
92 |
if (idx == 0) |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
93 |
throw new RuntimeException("No digits detected: " + size); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
94 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
95 |
result = Integer.parseInt(size.substring(0, idx)); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
96 |
|
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
97 |
if (idx < len) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
98 |
for (int i = idx; i < len; i++) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
99 |
switch(size.charAt(i)) { |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
100 |
case 'T': case 't': result *= K; // fall through |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
101 |
case 'G': case 'g': result *= K; // fall through |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
102 |
case 'M': case 'm': result *= K; // fall through |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
103 |
case 'K': case 'k': result *= K; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
104 |
break; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
105 |
default: |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
106 |
throw new RuntimeException("Unrecognized size: " + size); |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
107 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
108 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
109 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
110 |
return result; |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
111 |
} |
561a431cf238
6743526: (bf) -XX:MaxDirectMemorySize=<size> limits memory usage rather than total capacity as intended
alanb
parents:
diff
changeset
|
112 |
} |