2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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.
34 import java.util.Locale;
36 public class MemorySetting
38 public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
40 public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
42 private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
44 private static final long GIGABYTE = 1073741824; // 1GB
46 public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE / 2;
48 public static final long APPLICATION_MIN_MEMORY = GIGABYTE / 2;
50 private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
52 private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
54 protected static boolean logToClassChecked = false;
56 public static long getMemorySetting()
58 return getMemorySetting(null, null);
62 * Decide on appropriate memory setting for Jalview based on the two arguments
63 * values: jvmmempc - the maximum percentage of total physical memory to
64 * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
65 * allocate. These can be provided as arguments. If not provided as arguments
66 * (or set as null) system properties will be used instead (if set). The
67 * memory setting returned will be the lower of the two values. If either of
68 * the values are not provided then defaults will be used (jvmmempc=90,
69 * jvmmemmax=32GB). If total physical memory can't be ascertained when
70 * jvmmempc was set or neither jvmmempc nor jvmmemmax were set, then jvmmemmax
71 * defaults to a much safer 8GB. In this case explicitly setting jvmmemmax and
72 * not setting jvmmempc can set a higher memory for Jalview. The calculation
73 * also tries to ensure 0.5GB memory for the OS, but also tries to ensure at
74 * least 0.5GB memory for Jalview (which takes priority over the OS) If there
75 * is less then 0.5GB of physical memory then the total physical memory is
79 * Maximum value of memory to set. This can be a numeric string
80 * optionally followed by "b", "k", "m", "g", "t" (case insensitive)
81 * to indicate bytes, kilobytes, megabytes, gigabytes, terabytes
82 * respectively. If null a default value of 32G will be used. If null
83 * and either physical memory can't be determined then the default is
86 * Max percentage of physical memory to use. Defaults to "90".
88 * @return The amount of memory (in bytes) to allocate to Jalview
90 public static long getMemorySetting(String jvmmemmaxarg,
93 // actual Xmx value-to-be
96 // (absolute) jvmmaxmem setting, start with default
97 long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
98 if (jvmmemmaxarg == null)
100 jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
102 String jvmmemmax = jvmmemmaxarg;
103 if (jvmmemmax != null && jvmmemmax.length() > 0)
106 switch (jvmmemmax.toLowerCase(Locale.ROOT).substring(jvmmemmax.length() - 1))
109 multiplier = 1099511627776L; // 2^40
110 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
113 multiplier = 1073741824; // 2^30
114 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
117 multiplier = 1048576; // 2^20
118 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
121 multiplier = 1024; // 2^10
122 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
125 multiplier = 1; // 2^0
126 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
135 memmax = Long.parseLong(jvmmemmax);
136 } catch (NumberFormatException e)
138 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
139 System.out.println("MemorySetting Property '"
140 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
141 + "') badly formatted, using default ("
142 + MAX_HEAPSIZE_GB_DEFAULT + "g).");
145 // apply multiplier if not too big (i.e. bigger than a long)
146 if (Long.MAX_VALUE / memmax < multiplier)
148 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
149 System.out.println("MemorySetting Property '"
150 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
151 + ") too big, using default (" + MAX_HEAPSIZE_GB_DEFAULT
156 memmax = multiplier * memmax;
159 // check at least minimum value (this accounts for negatives too)
160 if (memmax < APPLICATION_MIN_MEMORY)
162 memmax = APPLICATION_MIN_MEMORY;
163 System.out.println("MemorySetting Property '"
164 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
165 + ") too small, using minimum (" + APPLICATION_MIN_MEMORY
172 // no need to warn if no setting
173 // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
178 // get max percent of physical memory, starting with default
179 float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
180 if (jvmmempcarg == null)
182 jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
184 String jvmmempc = jvmmempcarg;
188 if (jvmmempc != null)
190 float trypercent = Float.parseFloat(jvmmempc);
191 if (0 < trypercent && trypercent <= 100f)
193 percent = trypercent;
197 System.out.println("MemorySetting Property '"
198 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
199 + "' should be in range 1..100. Using default " + percent
203 } catch (NumberFormatException e)
205 System.out.println("MemorySetting property '"
206 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
207 + ") badly formatted");
210 // catch everything in case of no com.sun.management.OperatingSystemMXBean
211 boolean memoryPercentError = false;
214 long physicalMem = GetMemory.getPhysicalMemory();
215 if (physicalMem > APPLICATION_MIN_MEMORY)
217 // try and set at least applicationMinMemory and thereafter ensure
218 // leaveFreeMinMemory is left for the OS
220 mempc = (long) ((physicalMem / 100F) * percent);
222 // check for memory left for OS
223 boolean reducedmempc = false;
224 if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
226 mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
228 System.out.println("MemorySetting Property '"
229 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
230 + ") too large. Leaving free space for OS and reducing to ("
234 // check for minimum application memsize
235 if (mempc < APPLICATION_MIN_MEMORY)
239 System.out.println("Reduced MemorySetting (" + mempc
240 + ") too small. Increasing to application minimum ("
241 + APPLICATION_MIN_MEMORY + ").");
245 System.out.println("MemorySetting Property '"
246 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
247 + jvmmempcarg + ") too small. Using minimum ("
248 + APPLICATION_MIN_MEMORY + ").");
250 mempc = APPLICATION_MIN_MEMORY;
255 // not enough memory for application, just try and grab what we can!
258 "Not enough physical memory for application. Ignoring MemorySetting Property '"
259 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
261 + "). Using maximum memory available ("
262 + physicalMem + ").");
265 } catch (Throwable t)
267 memoryPercentError = true;
269 "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
273 // In the case of an error reading the percentage of physical memory (when
274 // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
276 if (memoryPercentError && mempc == -1
277 && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as
284 && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
287 "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
288 + "g due to failure to read physical memory size.");
289 memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
292 if (mempc == -1) // percentage memory not set
298 maxMemLong = Math.min(mempc, memmax);