如何找到程序使用的内存量?
How do I find the amount of memory used by a program?
我是 运行 BlueJ 中的几个 Java 程序。我想知道程序为给定的输入值使用了多少内存。有什么办法可以做到这一点吗?
1) 获取您的 Java 应用程序可以使用的最大内存量 Runtime:
Runtime runtime = Runtime.getRuntime();
System.out.println("max memory: " + runtime.maxMemory() / 1024);
2) 获取 JVM 为您的应用程序分配了多少内存:
Runtime runtime = Runtime.getRuntime();
System.out.println("allocated memory: " + runtime.totalMemory() / 1024);
3) 获取您的应用程序使用了多少内存:
Runtime runtime = Runtime.getRuntime();
System.out.println("free memory: " + runtime.freeMemory() / 1024);
可能是operating system specific; notably the JVM manages Java allocated memory, but you might call external functions in C++ using JNI which are allocating memory too (e.g. your Java program calling OpenCV functions), and you could be interested in the virtual address space of your current process. On Linux you might use proc(5) and read and parse /proc/self/statm
or /proc/self/stat
or /proc/self/status
. If your process has pid 1234, you could also try cat /proc/1234/maps
ijn an other terminal, or use pmap(1)。在其他操作系统上,您需要深入研究其相应的文档。
如果你只关心JVM直接使用的内存,使用Runtime class as .
顺便说一句,您的 JVM 有一个 garbage collector,因此定义所用内存的确切数量并非易事。
我是 运行 BlueJ 中的几个 Java 程序。我想知道程序为给定的输入值使用了多少内存。有什么办法可以做到这一点吗?
1) 获取您的 Java 应用程序可以使用的最大内存量 Runtime:
Runtime runtime = Runtime.getRuntime();
System.out.println("max memory: " + runtime.maxMemory() / 1024);
2) 获取 JVM 为您的应用程序分配了多少内存:
Runtime runtime = Runtime.getRuntime();
System.out.println("allocated memory: " + runtime.totalMemory() / 1024);
3) 获取您的应用程序使用了多少内存:
Runtime runtime = Runtime.getRuntime();
System.out.println("free memory: " + runtime.freeMemory() / 1024);
可能是operating system specific; notably the JVM manages Java allocated memory, but you might call external functions in C++ using JNI which are allocating memory too (e.g. your Java program calling OpenCV functions), and you could be interested in the virtual address space of your current process. On Linux you might use proc(5) and read and parse /proc/self/statm
or /proc/self/stat
or /proc/self/status
. If your process has pid 1234, you could also try cat /proc/1234/maps
ijn an other terminal, or use pmap(1)。在其他操作系统上,您需要深入研究其相应的文档。
如果你只关心JVM直接使用的内存,使用Runtime class as
顺便说一句,您的 JVM 有一个 garbage collector,因此定义所用内存的确切数量并非易事。