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 public static long getMemorySetting()
21 return getMemorySetting(null, null);
24 public static long getMemorySetting(String jvmmemmaxString,
25 String jvmmempcString)
27 // actual Xmx value-to-be
30 // get (absolute) jvmmaxmem setting
31 long memmax = maxHeapSizeDefault;
32 String jvmmemmaxorig = jvmmemmaxString;
33 if (jvmmemmaxorig == null)
35 jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
37 String jvmmemmax = jvmmemmaxorig;
38 if (jvmmemmax != null && jvmmemmax.length() > 0)
41 switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
44 multiplier = 1099511627776L; // 2^40
45 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
48 multiplier = 1073741824; // 2^30
49 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
52 multiplier = 1048576; // 2^20
53 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
56 multiplier = 1024; // 2^10
57 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
60 multiplier = 1; // 2^0
61 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
70 memmax = Long.parseLong(jvmmemmax);
71 } catch (NumberFormatException e)
73 memmax = maxHeapSizeDefault;
74 System.out.println("MemorySetting Property '" + maxHeapSizeProperty
76 + jvmmemmaxorig + "') badly formatted, using default ("
80 // apply multiplier if not too big (i.e. bigger than a long)
81 if (Long.MAX_VALUE / memmax < multiplier)
83 memmax = maxHeapSizeDefault;
85 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
87 + ") too big, using default (" + memmax + ").");
91 memmax = multiplier * memmax;
94 // check at least minimum value (this accounts for negatives too)
95 if (memmax < MemorySetting.applicationMinMemory)
97 memmax = MemorySetting.applicationMinMemory;
99 "MemorySetting Property '" + maxHeapSizeProperty + "' ("
101 + ") too small, using minimum (" + memmax + ").");
107 // no need to warn if no setting
108 // System.out.println("MemorySetting Property '" + maxHeapSizeProperty + "' not
112 // get max percent of physical memory
113 float percent = maxHeapSizePerCentDefault;
114 String jvmmempc = jvmmempcString;
115 if (jvmmempc == null)
117 jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
122 if (jvmmempc != null)
124 float trypercent = Float.parseFloat(jvmmempc);
125 if (0 < trypercent && trypercent <= 100f)
127 percent = trypercent;
132 "MemorySetting Property '" + maxHeapSizePerCentProperty
133 + "' should be in range 1..100");
136 } catch (NumberFormatException e)
139 "MemorySetting property '" + maxHeapSizePerCentProperty
140 + "' (" + jvmmempc + ") badly formatted");
143 // catch everything in case of no com.sun.management.OperatingSystemMXBean
144 boolean memoryPercentError = false;
147 pcmem = MemoryPercent.memPercent(percent);
148 } catch (Throwable t)
150 memoryPercentError = true;
151 System.out.println("Problem calling MemoryPercent.memPercent("
153 + "). Likely to be problem with com.sun.management.OperatingSystemMXBean");
156 // In the case of an error reading the percentage if physical memory, let's cap maxMemLong to 8GB
157 if (memoryPercentError && jvmmempc != null && pcmem == -1
158 && memmax > noMemMaxHeapSizeDefault)
161 "Capping maximum memory to 8GB due to failure to read physical memory size.");
162 memmax = noMemMaxHeapSizeDefault;
165 if (pcmem == -1) // not set
171 maxMemLong = Math.min(pcmem, memmax);