我有一个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);