JAL-3609 Added a -DforceHiDPISetting=true option mainly for testing, and updated...
[jalview.git] / getdown / src / getdown / core / src / main / java / 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 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   public static long getMemorySetting()
55   {
56     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   public static long getMemorySetting(String jvmmemmaxarg,
90           String jvmmempcarg)
91   {
92     // actual Xmx value-to-be
93     long maxMemLong = -1;
94
95     // (absolute) jvmmaxmem setting, start with default
96     long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
97     if (jvmmemmaxarg == null)
98     {
99       jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
100     }
101     String jvmmemmax = jvmmemmaxarg;
102     if (jvmmemmax != null && jvmmemmax.length() > 0)
103     {
104       long multiplier = 1;
105       switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
106       {
107       case "t":
108         multiplier = 1099511627776L; // 2^40
109         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
110         break;
111       case "g":
112         multiplier = 1073741824; // 2^30
113         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
114         break;
115       case "m":
116         multiplier = 1048576; // 2^20
117         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
118         break;
119       case "k":
120         multiplier = 1024; // 2^10
121         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
122         break;
123       case "b":
124         multiplier = 1; // 2^0
125         jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
126         break;
127       default:
128         break;
129       }
130
131       // parse the arg
132       try
133       {
134         memmax = Long.parseLong(jvmmemmax);
135       } catch (NumberFormatException e)
136       {
137         memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
138         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       if (Long.MAX_VALUE / memmax < multiplier)
147       {
148         memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
149         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         memmax = multiplier * memmax;
158       }
159
160       // check at least minimum value (this accounts for negatives too)
161       if (memmax < APPLICATION_MIN_MEMORY)
162       {
163         memmax = APPLICATION_MIN_MEMORY;
164         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     float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
182     if (jvmmempcarg == null)
183     {
184       jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
185     }
186     String jvmmempc = jvmmempcarg;
187     long mempc = -1;
188     try
189     {
190       if (jvmmempc != null)
191       {
192         float trypercent = Float.parseFloat(jvmmempc);
193         if (0 < trypercent && trypercent <= 100f)
194         {
195           percent = trypercent;
196         }
197         else
198         {
199           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       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     boolean memoryPercentError = false;
215     try
216     {
217       long physicalMem = GetMemory.getPhysicalMemory();
218       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         mempc = (long) ((physicalMem / 100F) * percent);
224
225         // check for memory left for OS
226         boolean reducedmempc = false;
227         if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
228         {
229           mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
230           reducedmempc = true;
231           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         if (mempc < APPLICATION_MIN_MEMORY)
239         {
240           if (reducedmempc)
241           {
242             System.out.println("Reduced MemorySetting (" + mempc
243                     + ") too small. Increasing to application minimum ("
244                     + APPLICATION_MIN_MEMORY + ").");
245           }
246           else
247           {
248             System.out.println("MemorySetting Property '"
249                     + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
250                     + ") too small. Using minimum (" + APPLICATION_MIN_MEMORY
251                     + ").");
252           }
253           mempc = APPLICATION_MIN_MEMORY;
254         }
255       }
256       else
257       {
258         // not enough memory for application, just try and grab what we can!
259         mempc = physicalMem;
260         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       memoryPercentError = true;
271       System.out.println(
272               "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
273       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     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       System.out.println(
285               "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
286                       + "g due to failure to read physical memory size.");
287       memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
288     }
289
290     if (mempc == -1) // percentage memory not set
291     {
292       maxMemLong = memmax;
293     }
294     else
295     {
296       maxMemLong = Math.min(mempc, memmax);
297     }
298
299     return maxMemLong;
300   }
301
302 }