+ if (jvmmempc != null)
+ {
+ int trypercent = Integer.parseInt(jvmmempc);
+ if (0 <= trypercent && trypercent <= 100)
+ {
+ percent = trypercent;
+ }
+ else
+ {
+ setAdjustmentMessage("MemorySetting Property '"
+ + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
+ + "' should be in range 0..100. Using default " + percent
+ + "%", quiet);
+ }
+ }
+ } catch (NumberFormatException e)
+ {
+ setAdjustmentMessage("MemorySetting property '"
+ + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
+ + ") badly formatted", quiet);
+ }
+
+ // catch everything in case of no com.sun.management.OperatingSystemMXBean
+ boolean memoryPercentError = false;
+ try
+ {
+ long physicalMem = GetMemory.getPhysicalMemory();
+ if (physicalMem > APPLICATION_MIN_MEMORY)
+ {
+ // try and set at least applicationMinMemory and thereafter ensure
+ // leaveFreeMinMemory is left for the OS
+
+ mempc = (long) ((physicalMem / 100F) * percent);
+
+ // check for memory left for OS
+ boolean reducedmempc = false;
+ if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
+ {
+ mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
+ reducedmempc = true;
+ setAdjustmentMessage("MemorySetting Property '"
+ + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
+ + ") too large. Leaving free space for OS and reducing to ("
+ + mempc + ").", quiet);
+ }
+
+ // check for minimum application memsize
+ if (mempc < APPLICATION_MIN_MEMORY)
+ {
+ if (reducedmempc)
+ {
+ setAdjustmentMessage("Reduced MemorySetting (" + mempc
+ + ") too small. Increasing to application minimum ("
+ + APPLICATION_MIN_MEMORY + ").", quiet);
+ }
+ else
+ {
+ setAdjustmentMessage("MemorySetting Property '"
+ + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
+ + jvmmempcarg + ") too small. Using minimum ("
+ + APPLICATION_MIN_MEMORY + ").", quiet);
+ }
+ mempc = APPLICATION_MIN_MEMORY;
+ }
+ }
+ else
+ {
+ // not enough memory for application, just try and grab what we can!
+ mempc = physicalMem;
+ setAdjustmentMessage(
+ "Not enough physical memory for application. Ignoring MemorySetting Property '"
+ + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
+ + jvmmempcarg
+ + "). Using maximum memory available ("
+ + physicalMem + ").",
+ quiet);
+ }
+
+ } catch (Throwable t)
+ {
+ memoryPercentError = true;
+ setAdjustmentMessage(
+ "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean",
+ quiet);
+ t.printStackTrace();
+ }
+
+ // In the case of an error reading the percentage of physical memory (when
+ // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
+ // maxMemLong to 8GB
+ if (memoryPercentError && mempc == -1
+ && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as
+ // (jvmmempcarg !=
+ // null ||
+ // (jvmmempcarg ==
+ // null &&
+ // jvmmemmaxarg
+ // == null))
+ && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
+ {
+ setAdjustmentMessage(
+ "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
+ + "g due to failure to read physical memory size.",
+ quiet);
+ memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
+ }
+
+ if (mempc == -1) // percentage memory not set
+ {
+ maxMemLong = memmax;
+ }
+ else
+ {
+ maxMemLong = Math.min(mempc, memmax);
+ }
+
+ return maxMemLong;
+ }
+
+ public static boolean isValidMemoryString(String text)
+ {
+ if (text.length() > 0)
+ {
+ char lastChar = text.charAt(text.length() - 1);
+ char[] otherChars = text.substring(0, text.length() - 1)
+ .toCharArray();
+ for (char c : otherChars)