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 public final static String maxHeapSizeProperty = "jvmmemmax";
17 public static long getMemorySetting()
19 return getMemorySetting(null, null);
22 public static long getMemorySetting(String jvmmemmaxString,
23 String jvmmempcString)
25 // actual Xmx value-to-be
28 // get (absolute) jvmmaxmem setting
29 long memmax = maxHeapSizeDefault;
30 String jvmmemmaxorig = jvmmemmaxString;
31 if (jvmmemmaxorig == null)
33 jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
35 String jvmmemmax = jvmmemmaxorig;
36 if (jvmmemmax != null && jvmmemmax.length() > 0)
39 switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
42 multiplier = 1099511627776L; // 2^40
43 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
46 multiplier = 1073741824; // 2^30
47 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
50 multiplier = 1048576; // 2^20
51 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
54 multiplier = 1024; // 2^10
55 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
58 multiplier = 1; // 2^0
59 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
68 memmax = Long.parseLong(jvmmemmax);
69 } catch (NumberFormatException e)
71 memmax = maxHeapSizeDefault;
72 System.out.println("MemorySetting Property '" + maxHeapSizeProperty
74 + jvmmemmaxorig + "') badly formatted, using default ("
78 // apply multiplier if not too big (i.e. bigger than a long)
79 if (Long.MAX_VALUE / memmax < multiplier)
81 memmax = maxHeapSizeDefault;
83 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
85 + ") too big, using default (" + memmax + ").");
89 memmax = multiplier * memmax;
92 // check at least minimum value (this accounts for negatives too)
93 if (memmax < MemorySetting.applicationMinMemory)
95 memmax = MemorySetting.applicationMinMemory;
97 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
99 + ") too small, using minimum (" + memmax + ").");
105 // no need to warn if no setting
106 // System.out.println("MemorySetting Property '" + maxHeapSizeProperty + "' not
110 // get max percent of physical memory
111 float percent = maxHeapSizePerCentDefault;
112 String jvmmempc = jvmmempcString;
113 if (jvmmempc == null)
115 jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
120 if (jvmmempc != null)
122 float trypercent = Float.parseFloat(jvmmempc);
123 if (0 < trypercent && trypercent <= 100f)
125 percent = trypercent;
130 "MemorySetting Property '" + maxHeapSizePerCentProperty
131 + "' should be in range 1..100");
134 } catch (NumberFormatException e)
137 "MemorySetting property '" + maxHeapSizePerCentProperty
138 + "' (" + jvmmempc + ") badly formatted");
141 // catch everything in case of no com.sun.management.OperatingSystemMXBean
142 boolean memoryPercentError = false;
145 pcmem = MemoryPercent.memPercent(percent);
146 } catch (Throwable t)
148 memoryPercentError = true;
149 System.out.println("Problem calling MemoryPercent.memPercent("
151 + "). Likely to be problem with com.sun.management.OperatingSystemMXBean");
154 // In the case of an error reading the percentage if physical memory, let's cap maxMemLong to 8GB
155 if (memoryPercentError && jvmmempc != null && pcmem == -1
156 && memmax > 8589934592L)
159 "Capping maximum memory to 8GB due to failure to read physical memory size.");
160 memmax = 8589934592L;
163 if (pcmem == -1) // not set
169 maxMemLong = Math.min(pcmem, memmax);