William Brendel によるこのサンプルは、役に立つかもしれません。
編集:私はもともとこのサンプルを提供しました(別のトピックに関するウィリアムブレンデルの回答へのリンク)。そのトピックの作成者 (Steve M) は、マルチプラットフォーム Java アプリケーションを作成したいと考えていました。具体的には、ユーザーは、実行中のマシンのリソース (ディスク容量、CPU、およびメモリ使用量) を評価する手段を見つけようとしていました。
これは、そのトピックで与えられた回答のインライン トランスクリプトです。ただし、このトピックについては、私の回答が承認済みとしてマークされているにもかかわらず、理想的な解決策ではないことが指摘されています。
public class Main {
public static void main(String[] args) {
/* Total number of processors or cores available to the JVM */
System.out.println("Available processors (cores): " +
Runtime.getRuntime().availableProcessors());
/* Total amount of free memory available to the JVM */
System.out.println("Free memory (bytes): " +
Runtime.getRuntime().freeMemory());
/* This will return Long.MAX_VALUE if there is no preset limit */
long maxMemory = Runtime.getRuntime().maxMemory();
/* Maximum amount of memory the JVM will attempt to use */
System.out.println("Maximum memory (bytes): " +
(maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
/* Total memory currently in use by the JVM */
System.out.println("Total memory (bytes): " +
Runtime.getRuntime().totalMemory());
/* Get a list of all filesystem roots on this system */
File[] roots = File.listRoots();
/* For each filesystem root, print some info */
for (File root : roots) {
System.out.println("File system root: " + root.getAbsolutePath());
System.out.println("Total space (bytes): " + root.getTotalSpace());
System.out.println("Free space (bytes): " + root.getFreeSpace());
System.out.println("Usable space (bytes): " + root.getUsableSpace());
}
}
}
Runtime.getRuntime().freeMemory()
ユーザーの Christian Friesは、メモリ不足エラーが発生するまでに割り当てられる可能性のあるメモリ量を で求めるのは間違っていると指摘しています。
documentationから、署名の戻り値は次のRuntime.getRuntime().freeMemory()
とおりです。
戻り値:将来割り当てられるオブジェクトに現在使用可能なメモリの総量の概算 (バイト単位)。
ただし、ユーザーの Christian Fries は、この関数は誤解されている可能性があると主張しています。彼は、メモリ不足エラーが発生するまでに割り当てられる可能性のあるメモリの概算量 (空きメモリ) は、次の式で与えられる可能性が高いと主張しています。
long presumableFreeMemory = Runtime.getRuntime().maxMemory() - allocatedMemory;
によってallocatedMemory
与えられます:
long allocatedMemory =
(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
ここで重要なのは、空きメモリの概念の不一致です。その 1 つは、オペレーティング システムが Java 仮想マシンに提供するメモリです。もう 1 つは、Java 仮想マシン自体によって実際に使用されているメモリ ブロックのチャンクを構成する合計バイト数です。
Java アプリケーションに割り当てられたメモリが Java 仮想マシンによってブロック単位で管理されることを考慮すると、Java 仮想マシンで使用できる空きメモリの量は、Java アプリケーションで使用できるメモリと正確に一致しない場合があります。
具体的には、Christian Fries は、-mx
または-Xmx
フラグを使用して、Java 仮想マシンで使用できるメモリの最大量を設定することを示しています。彼は、次の機能の違いに注目しています。
/* Returns the maximum amount of memory available to
the Java Virtual Machine set by the '-mx' or '-Xmx' flags. */
Runtime.getRuntime().maxMemory();
/* Returns the total memory allocated from the system
(which can at most reach the maximum memory value
returned by the previous function). */
Runtime.getRuntime().totalMemory();
/* Returns the free memory *within* the total memory
returned by the previous function. */
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().freeMemory()
クリスチャンは、実際には推定可能な空きメモリと呼ばれるものを返すと述べて、彼の答えを締めくくっています。将来のメモリ割り当てがその関数によって返される値を超えていなくても、Java 仮想マシンがホスト システムによって割り当てられた実際のメモリ チャンクをまだ受け取っていない場合は、java.lang.OutOfMemoryError
依然として が生成される可能性があります。
最終的に、使用する適切な方法は、アプリケーションの詳細に依存する度合いが異なります。
役に立つかもしれない別のリンクを提供します。これは、使用されるデフォルトの Java ヒープ サイズの決定について、ユーザー Richard Dormand によって作成され、stone333 によって回答された質問です。