JAL-3691 automatic insertion of Locale.ROOT to toUpperCase() and toLowerCase() and...
[jalview.git] / src / jalview / bin / MemorySetting.java
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 import java.util.Locale;
35
36 public class MemorySetting
37 {
38   public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
39
40   public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
41
42   private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
43
44   private static final long GIGABYTE = 1073741824; // 1GB
45
46   public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE / 2;
47
48   public static final long APPLICATION_MIN_MEMORY = GIGABYTE / 2;
49
50   private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
51
52   private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
53
54   protected static boolean logToClassChecked = false;
55
56   public static long getMemorySetting()
57   {
58     return getMemorySetting(null, null);
59   }
60
61   /**
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
76    * used for Jalview.
77    * 
78    * @param jvmmemmaxarg
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
84    *          8GB.
85    * @param jvmmempcarg
86    *          Max percentage of physical memory to use. Defaults to "90".
87    * 
88    * @return The amount of memory (in bytes) to allocate to Jalview
89    */
90   public static long getMemorySetting(String jvmmemmaxarg,
91           String jvmmempcarg)
92   {
93     // actual Xmx value-to-be
94     long maxMemLong = -1;
95
96     // (absolute) jvmmaxmem setting, start with default
97     long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
98     if (jvmmemmaxarg == null)
99     {
100       jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
101     }
102     String jvmmemmax = jvmmemmaxarg;
103     if (jvmmemmax != null && jvmmemmax.length() > 0)
104     {
105       long multiplier = 1;
106       switch (jvmmemmax.toLowerCase(Locale.ROOT).substring(jvmmemmax.length() - 1))
107       {
108       case "t":
109         multiplier = 1099511627776L; // 2^40
110         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
111         break;
112       case "g":
113         multiplier = 1073741824; // 2^30
114         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
115         break;
116       case "m":
117         multiplier = 1048576; // 2^20
118         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
119         break;
120       case "k":
121         multiplier = 1024; // 2^10
122         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
123         break;
124       case "b":
125         multiplier = 1; // 2^0
126         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
127         break;
128       default:
129         break;
130       }
131
132       // parse the arg
133       try
134       {
135         memmax = Long.parseLong(jvmmemmax);
136       } catch (NumberFormatException e)
137       {
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).");
143       }
144
145       // apply multiplier if not too big (i.e. bigger than a long)
146       if (Long.MAX_VALUE / memmax < multiplier)
147       {
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
152                 + "g).");
153       }
154       else
155       {
156         memmax = multiplier * memmax;
157       }
158
159       // check at least minimum value (this accounts for negatives too)
160       if (memmax < APPLICATION_MIN_MEMORY)
161       {
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
166                 + ").");
167       }
168
169     }
170     else
171     {
172       // no need to warn if no setting
173       // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
174       // + "' not
175       // set.");
176     }
177
178     // get max percent of physical memory, starting with default
179     float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
180     if (jvmmempcarg == null)
181     {
182       jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
183     }
184     String jvmmempc = jvmmempcarg;
185     long mempc = -1;
186     try
187     {
188       if (jvmmempc != null)
189       {
190         float trypercent = Float.parseFloat(jvmmempc);
191         if (0 < trypercent && trypercent <= 100f)
192         {
193           percent = trypercent;
194         }
195         else
196         {
197           System.out.println("MemorySetting Property '"
198                   + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
199                   + "' should be in range 1..100. Using default " + percent
200                   + "%");
201         }
202       }
203     } catch (NumberFormatException e)
204     {
205       System.out.println("MemorySetting property '"
206               + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
207               + ") badly formatted");
208     }
209
210     // catch everything in case of no com.sun.management.OperatingSystemMXBean
211     boolean memoryPercentError = false;
212     try
213     {
214       long physicalMem = GetMemory.getPhysicalMemory();
215       if (physicalMem > APPLICATION_MIN_MEMORY)
216       {
217         // try and set at least applicationMinMemory and thereafter ensure
218         // leaveFreeMinMemory is left for the OS
219
220         mempc = (long) ((physicalMem / 100F) * percent);
221
222         // check for memory left for OS
223         boolean reducedmempc = false;
224         if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
225         {
226           mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
227           reducedmempc = true;
228           System.out.println("MemorySetting Property '"
229                   + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
230                   + ") too large. Leaving free space for OS and reducing to ("
231                   + mempc + ").");
232         }
233
234         // check for minimum application memsize
235         if (mempc < APPLICATION_MIN_MEMORY)
236         {
237           if (reducedmempc)
238           {
239             System.out.println("Reduced MemorySetting (" + mempc
240                     + ") too small. Increasing to application minimum ("
241                     + APPLICATION_MIN_MEMORY + ").");
242           }
243           else
244           {
245             System.out.println("MemorySetting Property '"
246                     + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
247                     + jvmmempcarg + ") too small. Using minimum ("
248                     + APPLICATION_MIN_MEMORY + ").");
249           }
250           mempc = APPLICATION_MIN_MEMORY;
251         }
252       }
253       else
254       {
255         // not enough memory for application, just try and grab what we can!
256         mempc = physicalMem;
257         System.out.println(
258                 "Not enough physical memory for application. Ignoring MemorySetting Property '"
259                         + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
260                         + jvmmempcarg
261                         + "). Using maximum memory available ("
262                         + physicalMem + ").");
263       }
264
265     } catch (Throwable t)
266     {
267       memoryPercentError = true;
268       System.out.println(
269               "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
270       t.printStackTrace();
271     }
272
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
275     // maxMemLong to 8GB
276     if (memoryPercentError && mempc == -1
277             && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as
278                                                               // (jvmmempcarg !=
279                                                               // null ||
280                                                               // (jvmmempcarg ==
281                                                               // null &&
282                                                               // jvmmemmaxarg
283                                                               // == null))
284             && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
285     {
286       System.out.println(
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;
290     }
291
292     if (mempc == -1) // percentage memory not set
293     {
294       maxMemLong = memmax;
295     }
296     else
297     {
298       maxMemLong = Math.min(mempc, memmax);
299     }
300
301     return maxMemLong;
302   }
303
304 }