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 jvmmemmaxorig,
29 // actual Xmx value-to-be
32 // get (absolute) jvmmaxmem setting
33 long memmax = maxHeapSizeDefault;
34 if (jvmmemmaxorig == null)
36 jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
38 String jvmmemmax = jvmmemmaxorig;
39 if (jvmmemmax != null && jvmmemmax.length() > 0)
42 switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
45 multiplier = 1099511627776L; // 2^40
46 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
49 multiplier = 1073741824; // 2^30
50 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
53 multiplier = 1048576; // 2^20
54 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
57 multiplier = 1024; // 2^10
58 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
61 multiplier = 1; // 2^0
62 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
71 memmax = Long.parseLong(jvmmemmax);
72 } catch (NumberFormatException e)
74 memmax = maxHeapSizeDefault;
75 System.out.println("MemorySetting Property '"
78 + jvmmemmaxorig + "') badly formatted, using default ("
82 // apply multiplier if not too big (i.e. bigger than a long)
83 if (Long.MAX_VALUE / memmax < multiplier)
85 memmax = maxHeapSizeDefault;
87 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
89 + ") too big, using default (" + memmax + ").");
93 memmax = multiplier * memmax;
96 // check at least minimum value (this accounts for negatives too)
97 if (memmax < applicationMinMemory)
99 memmax = applicationMinMemory;
101 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
103 + ") too small, using minimum ("
104 + applicationMinMemory + ").");
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 if (jvmmempcorig == null)
120 jvmmempcorig = System.getProperty(maxHeapSizePerCentProperty);
122 String jvmmempc = jvmmempcorig;
123 if (jvmmempc == null)
125 jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
130 if (jvmmempc != null)
132 float trypercent = Float.parseFloat(jvmmempc);
133 if (0 < trypercent && trypercent <= 100f)
135 percent = trypercent;
140 "MemorySetting Property '" + maxHeapSizePerCentProperty
141 + "' should be in range 1..100");
144 } catch (NumberFormatException e)
147 "MemorySetting property '" + maxHeapSizePerCentProperty
148 + "' (" + jvmmempc + ") badly formatted");
151 // catch everything in case of no com.sun.management.OperatingSystemMXBean
152 boolean memoryPercentError = false;
155 long physicalMem = MemoryPercent.getPhysicalMemory();
156 if (physicalMem > applicationMinMemory)
158 // try and set at least applicationMinMemory and thereafter ensure
159 // leaveFreeMinMemory is left for the OS
161 pcmem = (long) ((physicalMem * percent) / 100F);
163 // check for memory left for OS
164 if (physicalMem - pcmem < leaveFreeMinMemory)
166 pcmem = physicalMem - leaveFreeMinMemory;
167 System.out.println("MemorySetting Property '"
168 + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
169 + ") too large. Leaving free space for OS, using ("
173 // check for minimum application memsize
174 if (pcmem < applicationMinMemory)
176 pcmem = applicationMinMemory;
177 System.out.println("MemorySetting Property '"
178 + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
179 + ") too small, using minimum (" + applicationMinMemory
185 // not enough memory for application, just try and grab what we can!
187 System.out.println("MemorySetting Property '"
188 + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
189 + "): Not enough memory, using max available (" + pcmem
193 } catch (Throwable t)
195 memoryPercentError = true;
196 System.out.println("Problem calling MemoryPercent.memPercent("
198 + "). Likely to be problem with com.sun.management.OperatingSystemMXBean");
201 // In the case of an error reading the percentage of physical memory (when jvmmempc was set), let's cap maxMemLong to 8GB
202 if (memoryPercentError && jvmmempc != null && pcmem == -1
203 && memmax > noMemMaxHeapSizeDefault)
206 "Capping maximum memory to 8GB due to failure to read physical memory size.");
207 memmax = noMemMaxHeapSizeDefault;
210 if (pcmem == -1) // not set
216 maxMemLong = Math.min(pcmem, memmax);