2019年11月2日
Java 仮想マシンのメモリ情報について、「合計」、「使用量」、「使用可能最大」の情報を返します。
各項目の説明は以下となります。
/** * Java 仮想マシンのメモリ総容量、使用量、 * 使用を試みる最大メモリ容量の情報を返します。 * @return Java 仮想マシンのメモリ情報 */ public static String getMemoryInfo() { DecimalFormat f1 = new DecimalFormat("#,###KB"); DecimalFormat f2 = new DecimalFormat("##.#"); long free = Runtime.getRuntime().freeMemory() / 1024; long total = Runtime.getRuntime().totalMemory() / 1024; long max = Runtime.getRuntime().maxMemory() / 1024; long used = total - free; double ratio = (used * 100 / (double)total); String info = "Java メモリ情報 : 合計=" + f1.format(total) + "、" + "使用量=" + f1.format(used) + " (" + f2.format(ratio) + "%)、" + "使用可能最大="+f1.format(max); return info; }
public static void main(String[] args) { System.out.println(getMemoryInfo()); }◆出力結果
Java メモリ情報 : 合計=1,984KB、使用量=458KB (23.1%)、使用可能最大=65,088KB
public static void main(String[] args) { System.out.println(getMemoryInfo()); }◆出力結果
Java メモリ情報 : 合計=65,088KB、使用量=524KB (0.8%)、使用可能最大=520,256KB「合計 Runtime.getRuntime().totalMemory()」が-Xmsに、「使用可能最大 Runtime.getRuntime().maxMemory()」が-Xmxにリンクしています。