/** * Returns the heap size that was specified by the -Xmx value from the user, or an approximated * value if the -Xmx value was not set or was set improperly. */ public static long getUserDefinedHeapSize() { for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) { if (arg.startsWith("-Xmx")) { Optional<Long> heapSize = parseSizeToBytes(arg.substring("-Xmx".length())); if (heapSize.isPresent()) { return heapSize.get(); } break; } } // If the -Xmx value was not set or was set improperly, get an approximation of the // heap size long heapSize = 0; for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) { if (mpBean.getType() == MemoryType.HEAP) { heapSize += mpBean.getUsage().getMax(); } } return heapSize; }
Optional<Long> heapSize = parseSizeToBytes(dexOptions.getJavaMaxHeapSize()); if (heapSize.isPresent()) { requestedHeapSize = heapSize.get();