我有一个java应用程序。现在的问题是我使用 jmap 获取了一个堆,并且我还在我的应用程序中运行了这些代码。两者都给了我不同的值。运行时显示 256mb
这是我分配的初始内存和最大内存?我想检测是否存在内存泄漏,但运行时间在波动,而堆中的内存不断少量增加?有什么帮助吗?
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("\n\nUsed memory is bytes: " + memory);
//Print the jvm heap size.
long heapSize = runtime.totalMemory();
System.out.println("\n\nHeap Size = " + heapSize);
int mb = 1024*1024;
System.out.println("##### Heap utilization statistics [MB] #####");
//Print used memory
System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);
//Print free memory
System.out.println("Free Memory:" + runtime.freeMemory() / mb);
//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);
//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);
最佳答案
我建议使用Visual VM查看内存使用情况等。
关于java - 哪一项指示了 java 应用程序的确切内存使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18455995/