Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.bin

File MemorySetting.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

26
81
2
1
302
194
29
0.36
40.5
2
14.5

Classes

Class Line # Actions
MemorySetting 34 81 29
0.8256880682.6%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.bin;
22   
23    /**
24    * Methods to decide on appropriate memory setting for Jalview based on two
25    * optionally provided values: jvmmempc - the maximum percentage of total
26    * physical memory to allocate, and jvmmemmax - the maximum absolute amount of
27    * physical memory to allocate. These can be provided as arguments or system
28    * properties. Other considerations such as minimum application requirements and
29    * leaving space for OS are used too.
30    *
31    * @author bsoares
32    *
33    */
 
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; // 90%
41   
42    private static final long GIGABYTE = 1073741824; // 1GB
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   
 
54  1 toggle public static long getMemorySetting()
55    {
56  1 return getMemorySetting(null, null);
57    }
58   
59    /**
60    * Decide on appropriate memory setting for Jalview based on the two arguments
61    * values: jvmmempc - the maximum percentage of total physical memory to
62    * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
63    * allocate. These can be provided as arguments. If not provided as arguments
64    * (or set as null) system properties will be used instead (if set). The memory
65    * setting returned will be the lower of the two values. If either of the values
66    * are not provided then defaults will be used (jvmmempc=90, jvmmemmax=32GB). If
67    * total physical memory can't be ascertained when jvmmempc was set or neither
68    * jvmmempc nor jvmmemmax were set, then jvmmemmax defaults to a much safer 8GB.
69    * In this case explicitly setting jvmmemmax and not setting jvmmempc can set a
70    * higher memory for Jalview. The calculation also tries to ensure 0.5GB memory
71    * for the OS, but also tries to ensure at least 0.5GB memory for Jalview (which
72    * takes priority over the OS) If there is less then 0.5GB of physical memory
73    * then the total physical memory is used for Jalview.
74    *
75    * @param jvmmemmaxarg
76    * Maximum value of memory to set. This can be a numeric
77    * string optionally followed by "b", "k", "m", "g", "t"
78    * (case insensitive) to indicate bytes, kilobytes,
79    * megabytes, gigabytes, terabytes respectively. If null a
80    * default value of 32G will be used. If null and either
81    * physical memory can't be determined then the default is
82    * 8GB.
83    * @param jvmmempcarg
84    * Max percentage of physical memory to use. Defaults to
85    * "90".
86    *
87    * @return The amount of memory (in bytes) to allocate to Jalview
88    */
 
89  16 toggle public static long getMemorySetting(String jvmmemmaxarg,
90    String jvmmempcarg)
91    {
92    // actual Xmx value-to-be
93  16 long maxMemLong = -1;
94   
95    // (absolute) jvmmaxmem setting, start with default
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; // 2^40
109  3 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
110  3 break;
111  2 case "g":
112  2 multiplier = 1073741824; // 2^30
113  2 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
114  2 break;
115  2 case "m":
116  2 multiplier = 1048576; // 2^20
117  2 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
118  2 break;
119  2 case "k":
120  2 multiplier = 1024; // 2^10
121  2 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
122  2 break;
123  5 case "b":
124  5 multiplier = 1; // 2^0
125  5 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
126  5 break;
127  1 default:
128  1 break;
129    }
130   
131    // parse the arg
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    // apply multiplier if not too big (i.e. bigger than a long)
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    // check at least minimum value (this accounts for negatives too)
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    // no need to warn if no setting
175    // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
176    // + "' not
177    // set.");
178    }
179   
180    // get max percent of physical memory, starting with default
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    // catch everything in case of no com.sun.management.OperatingSystemMXBean
214  16 boolean memoryPercentError = false;
215  16 try
216    {
217  16 long physicalMem = GetMemory.getPhysicalMemory();
218  16 if (physicalMem > APPLICATION_MIN_MEMORY)
219    {
220    // try and set at least applicationMinMemory and thereafter ensure
221    // leaveFreeMinMemory is left for the OS
222   
223  16 mempc = (long) ((physicalMem / 100F) * percent);
224   
225    // check for memory left for OS
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    // check for minimum application memsize
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    // not enough memory for application, just try and grab what we can!
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    // In the case of an error reading the percentage of physical memory (when
277    // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
278    // maxMemLong to 8GB
279  16 if (memoryPercentError && mempc == -1
280    && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg
281    // == null))
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) // percentage memory not set
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    }