eclipse 用eclipse测试java程序的内存消耗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5869432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
test memory consumption of a java program with eclipse
提问by user685275
Is there a plugin in eclipse
that I could use to test who much memory has my just run program cost?
是否有一个插件eclipse
可以用来测试我刚刚运行的程序消耗了多少内存?
I am thinking there may be a button from the plugin after I run the program, I could click on it, and it shows me a graph of sort of the peak memory consumption of my program just now.
我想在我运行程序后插件中可能会有一个按钮,我可以点击它,它向我显示了我的程序刚才的峰值内存消耗的图表。
回答by Mr. Nobody
回答by ChrisH
I agree with Mr. Nobody that VisualVM is nice. The Eclipse Memory Analyzerhas some nice features as well.
我同意没有人先生的观点,VisualVM 很好。在Eclipse的内存分析器有一些不错的功能,以及。
回答by nazar_art
The total used / free memoryof a program can be obtained in the program via java.lang.Runtime.getRuntime()
;
可以通过以下方式在程序中获得程序的总已用/可用内存java.lang.Runtime.getRuntime()
;
The runtime has several methods which relate to the memory. The following coding example demonstrates its usage.
运行时有几个与内存相关的方法。下面的编码示例演示了它的用法。
import java.util.ArrayList;
import java.util.List;
public class PerformanceTest {
private static final long MEGABYTE = 1024L * 1024L;
public static long bytesToMegabytes(long bytes) {
return bytes / MEGABYTE;
}
public static void main(String[] args) {
// I assume you will know how to create an object Person yourself...
List<Person> list = new ArrayList<Person>();
for (int i = 0; i <= 100000; i++) {
list.add(new Person("Jim", "Knopf"));
}
// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: "
+ bytesToMegabytes(memory));
}
}