1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.bin; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
@author |
32 |
|
|
33 |
|
|
|
|
| 82.6% |
Uncovered Elements: 19 (109) |
Complexity: 29 |
Complexity Density: 0.36 |
|
34 |
|
public class MemorySetting |
35 |
|
{ |
36 |
|
public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc"; |
37 |
|
|
38 |
|
public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax"; |
39 |
|
|
40 |
|
private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; |
41 |
|
|
42 |
|
private static final long GIGABYTE = 1073741824; |
43 |
|
|
44 |
|
public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE/2; |
45 |
|
|
46 |
|
public static final long APPLICATION_MIN_MEMORY = GIGABYTE/2; |
47 |
|
|
48 |
|
private static final long MAX_HEAPSIZE_GB_DEFAULT = 32; |
49 |
|
|
50 |
|
private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8; |
51 |
|
|
52 |
|
protected static boolean logToClassChecked = false; |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
1 |
public static long getMemorySetting()... |
55 |
|
{ |
56 |
1 |
return getMemorySetting(null, null); |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@param |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@param |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@return |
88 |
|
|
|
|
| 82.1% |
Uncovered Elements: 19 (106) |
Complexity: 28 |
Complexity Density: 0.35 |
|
89 |
16 |
public static long getMemorySetting(String jvmmemmaxarg,... |
90 |
|
String jvmmempcarg) |
91 |
|
{ |
92 |
|
|
93 |
16 |
long maxMemLong = -1; |
94 |
|
|
95 |
|
|
96 |
16 |
long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; |
97 |
16 |
if (jvmmemmaxarg == null) |
98 |
|
{ |
99 |
1 |
jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME); |
100 |
|
} |
101 |
16 |
String jvmmemmax = jvmmemmaxarg; |
102 |
16 |
if (jvmmemmax != null && jvmmemmax.length() > 0) |
103 |
|
{ |
104 |
15 |
long multiplier = 1; |
105 |
15 |
switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1)) |
106 |
|
{ |
107 |
3 |
case "t": |
108 |
3 |
multiplier = 1099511627776L; |
109 |
3 |
jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1); |
110 |
3 |
break; |
111 |
2 |
case "g": |
112 |
2 |
multiplier = 1073741824; |
113 |
2 |
jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1); |
114 |
2 |
break; |
115 |
2 |
case "m": |
116 |
2 |
multiplier = 1048576; |
117 |
2 |
jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1); |
118 |
2 |
break; |
119 |
2 |
case "k": |
120 |
2 |
multiplier = 1024; |
121 |
2 |
jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1); |
122 |
2 |
break; |
123 |
5 |
case "b": |
124 |
5 |
multiplier = 1; |
125 |
5 |
jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1); |
126 |
5 |
break; |
127 |
1 |
default: |
128 |
1 |
break; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
15 |
try |
133 |
|
{ |
134 |
15 |
memmax = Long.parseLong(jvmmemmax); |
135 |
|
} catch (NumberFormatException e) |
136 |
|
{ |
137 |
2 |
memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; |
138 |
2 |
System.out.println("MemorySetting Property '" |
139 |
|
+ MAX_HEAPSIZE_PROPERTY_NAME |
140 |
|
+ "' (" |
141 |
|
+ jvmmemmaxarg + "') badly formatted, using default (" |
142 |
|
+ MAX_HEAPSIZE_GB_DEFAULT + "g)."); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
15 |
if (Long.MAX_VALUE / memmax < multiplier) |
147 |
|
{ |
148 |
0 |
memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; |
149 |
0 |
System.out.println( |
150 |
|
"MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' (" |
151 |
|
+ jvmmemmaxarg |
152 |
|
+ ") too big, using default (" |
153 |
|
+ MAX_HEAPSIZE_GB_DEFAULT + "g)."); |
154 |
|
} |
155 |
|
else |
156 |
|
{ |
157 |
15 |
memmax = multiplier * memmax; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
15 |
if (memmax < APPLICATION_MIN_MEMORY) |
162 |
|
{ |
163 |
1 |
memmax = APPLICATION_MIN_MEMORY; |
164 |
1 |
System.out.println( |
165 |
|
"MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' (" |
166 |
|
+ jvmmemmaxarg |
167 |
|
+ ") too small, using minimum (" |
168 |
|
+ APPLICATION_MIN_MEMORY + ")."); |
169 |
|
} |
170 |
|
|
171 |
|
} |
172 |
|
else |
173 |
|
{ |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
16 |
float percent = MAX_HEAPSIZE_PERCENT_DEFAULT; |
182 |
16 |
if (jvmmempcarg == null) |
183 |
|
{ |
184 |
1 |
jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME); |
185 |
|
} |
186 |
16 |
String jvmmempc = jvmmempcarg; |
187 |
16 |
long mempc = -1; |
188 |
16 |
try |
189 |
|
{ |
190 |
16 |
if (jvmmempc != null) |
191 |
|
{ |
192 |
15 |
float trypercent = Float.parseFloat(jvmmempc); |
193 |
15 |
if (0 < trypercent && trypercent <= 100f) |
194 |
|
{ |
195 |
15 |
percent = trypercent; |
196 |
|
} |
197 |
|
else |
198 |
|
{ |
199 |
0 |
System.out.println( |
200 |
|
"MemorySetting Property '" |
201 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME |
202 |
|
+ "' should be in range 1..100. Using default " |
203 |
|
+ percent + "%"); |
204 |
|
} |
205 |
|
} |
206 |
|
} catch (NumberFormatException e) |
207 |
|
{ |
208 |
0 |
System.out.println( |
209 |
|
"MemorySetting property '" + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME |
210 |
|
+ "' (" + jvmmempcarg + ") badly formatted"); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
16 |
boolean memoryPercentError = false; |
215 |
16 |
try |
216 |
|
{ |
217 |
16 |
long physicalMem = GetMemory.getPhysicalMemory(); |
218 |
16 |
if (physicalMem > APPLICATION_MIN_MEMORY) |
219 |
|
{ |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
16 |
mempc = (long) ((physicalMem / 100F) * percent); |
224 |
|
|
225 |
|
|
226 |
16 |
boolean reducedmempc = false; |
227 |
16 |
if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY) |
228 |
|
{ |
229 |
12 |
mempc = physicalMem - LEAVE_FREE_MIN_MEMORY; |
230 |
12 |
reducedmempc = true; |
231 |
12 |
System.out.println("MemorySetting Property '" |
232 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg |
233 |
|
+ ") too large. Leaving free space for OS and reducing to (" |
234 |
|
+ mempc + ")."); |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
16 |
if (mempc < APPLICATION_MIN_MEMORY) |
239 |
|
{ |
240 |
1 |
if (reducedmempc) |
241 |
|
{ |
242 |
0 |
System.out.println("Reduced MemorySetting (" + mempc |
243 |
|
+ ") too small. Increasing to application minimum (" |
244 |
|
+ APPLICATION_MIN_MEMORY + ")."); |
245 |
|
} |
246 |
|
else |
247 |
|
{ |
248 |
1 |
System.out.println("MemorySetting Property '" |
249 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg |
250 |
|
+ ") too small. Using minimum (" + APPLICATION_MIN_MEMORY |
251 |
|
+ ")."); |
252 |
|
} |
253 |
1 |
mempc = APPLICATION_MIN_MEMORY; |
254 |
|
} |
255 |
|
} |
256 |
|
else |
257 |
|
{ |
258 |
|
|
259 |
0 |
mempc = physicalMem; |
260 |
0 |
System.out.println( |
261 |
|
"Not enough physical memory for application. Ignoring MemorySetting Property '" |
262 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" |
263 |
|
+ jvmmempcarg |
264 |
|
+ "). Using maximum memory available (" |
265 |
|
+ physicalMem + ")."); |
266 |
|
} |
267 |
|
|
268 |
|
} catch (Throwable t) |
269 |
|
{ |
270 |
0 |
memoryPercentError = true; |
271 |
0 |
System.out.println( |
272 |
|
"Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean"); |
273 |
0 |
t.printStackTrace(); |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
16 |
if (memoryPercentError && mempc == -1 |
280 |
|
&& !(jvmmempcarg == null && jvmmemmaxarg != null) |
281 |
|
|
282 |
|
&& memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE) |
283 |
|
{ |
284 |
0 |
System.out.println( |
285 |
|
"Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT |
286 |
|
+ "g due to failure to read physical memory size."); |
287 |
0 |
memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; |
288 |
|
} |
289 |
|
|
290 |
16 |
if (mempc == -1) |
291 |
|
{ |
292 |
0 |
maxMemLong = memmax; |
293 |
|
} |
294 |
|
else |
295 |
|
{ |
296 |
16 |
maxMemLong = Math.min(mempc, memmax); |
297 |
|
} |
298 |
|
|
299 |
16 |
return maxMemLong; |
300 |
|
} |
301 |
|
|
302 |
|
} |