3 public class MemorySetting
5 public static final long leaveFreeMinMemory = 536870912; // 0.5 GB
7 public static final long applicationMinMemory = 536870912; // 0.5 GB
9 private final static int maxHeapSizePerCentDefault = 90;
11 public final static String maxHeapSizePerCentProperty = "jvmmempc";
13 private final static long maxHeapSizeDefault = 34359738368L; // 32GB
15 private final static long noMemMaxHeapSizeDefault = 8589934592L; // 8GB
17 public final static String maxHeapSizeProperty = "jvmmemmax";
19 protected static boolean logToClassChecked = false;
21 public static long getMemorySetting()
23 return getMemorySetting(null, null);
26 public static long getMemorySetting(String jvmmemmaxString,
27 String jvmmempcString)
29 // actual Xmx value-to-be
32 // get (absolute) jvmmaxmem setting
33 long memmax = maxHeapSizeDefault;
34 String jvmmemmaxorig = jvmmemmaxString;
35 if (jvmmemmaxorig == null)
37 jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
39 String jvmmemmax = jvmmemmaxorig;
40 if (jvmmemmax != null && jvmmemmax.length() > 0)
43 switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
46 multiplier = 1099511627776L; // 2^40
47 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
50 multiplier = 1073741824; // 2^30
51 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
54 multiplier = 1048576; // 2^20
55 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
58 multiplier = 1024; // 2^10
59 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
62 multiplier = 1; // 2^0
63 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
72 memmax = Long.parseLong(jvmmemmax);
73 } catch (NumberFormatException e)
75 memmax = maxHeapSizeDefault;
76 System.out.println("MemorySetting Property '"
79 + jvmmemmaxorig + "') badly formatted, using default ("
83 // apply multiplier if not too big (i.e. bigger than a long)
84 if (Long.MAX_VALUE / memmax < multiplier)
86 memmax = maxHeapSizeDefault;
88 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
90 + ") too big, using default (" + memmax + ").");
94 memmax = multiplier * memmax;
97 // check at least minimum value (this accounts for negatives too)
98 if (memmax < MemorySetting.applicationMinMemory)
100 memmax = MemorySetting.applicationMinMemory;
102 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
104 + ") too small, using minimum (" + memmax + ").");
110 // no need to warn if no setting
111 // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
116 // get max percent of physical memory
117 float percent = maxHeapSizePerCentDefault;
118 String jvmmempc = jvmmempcString;
119 if (jvmmempc == null)
121 jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
126 if (jvmmempc != null)
128 float trypercent = Float.parseFloat(jvmmempc);
129 if (0 < trypercent && trypercent <= 100f)
131 percent = trypercent;
136 "MemorySetting Property '" + maxHeapSizePerCentProperty
137 + "' should be in range 1..100");
140 } catch (NumberFormatException e)
143 "MemorySetting property '" + maxHeapSizePerCentProperty
144 + "' (" + jvmmempc + ") badly formatted");
147 // catch everything in case of no com.sun.management.OperatingSystemMXBean
148 boolean memoryPercentError = false;
151 pcmem = MemoryPercent.memPercentAmount(percent);
152 } catch (Throwable t)
154 memoryPercentError = true;
155 System.out.println("Problem calling MemoryPercent.memPercent("
157 + "). Likely to be problem with com.sun.management.OperatingSystemMXBean");
160 // In the case of an error reading the percentage of physical memory (when jvmmempc was set), let's cap maxMemLong to 8GB
161 if (memoryPercentError && jvmmempc != null && pcmem == -1
162 && memmax > noMemMaxHeapSizeDefault)
165 "Capping maximum memory to 8GB due to failure to read physical memory size.");
166 memmax = noMemMaxHeapSizeDefault;
169 if (pcmem == -1) // not set
175 maxMemLong = Math.min(pcmem, memmax);