private static long[] getMemoryInfo() { final long[] memory = new long[Type.values().length]; memory[Type.FREE.ordinal()] = Runtime.getRuntime().freeMemory(); memory[Type.TOTAL.ordinal()] = Runtime.getRuntime().totalMemory(); memory[Type.MAX.ordinal()] = Runtime.getRuntime().maxMemory(); return memory; }
/** * Get information about the current memory status of the JVM. * * @param unit used for formatting. Valid values are: * <ul> * <li>{@link Unit#BYTES} * <li>{@link Unit#KILOBYTES} * <li>{@link Unit#KIBIBYTES} * <li>{@link Unit#MEGABYTES} * <li>{@link Unit#MEBIBYTES} * </ul> * If no value is given, {@link Unit#BYTES} will be used. No decimal place will be calculated, * plain integer values are returned. * @return a string with the current memory information */ public static String getMemoryInfo(final Unit unit) { final long[] memory = getMemoryInfo(); for (final Type type : Type.values()) { memory[type.ordinal()] /= unit.getDenominator(); } final StringBuilder sb = new StringBuilder(100); sb.append("Memory (free/total/max): "); sb.append(memory[Type.FREE.ordinal()]); sb.append(unit.getUnitString()); sb.append("/"); sb.append(memory[Type.TOTAL.ordinal()]); sb.append(unit.getUnitString()); sb.append("/"); sb.append(memory[Type.MAX.ordinal()]); sb.append(unit.getUnitString()); return sb.toString(); }