Getting Memory Statistics in Java: Runtime vs. MemoryMXBean

三世轮回 提交于 2020-06-24 08:41:05

问题


I am creating a scheduled executor to read the memory usage of the JVM. I have come across two ways to get memory statistics in a running JVM - Runtime & MemoryMXBean, with the following correspondence between their methods:

memoryMxBean.getHeapMemoryUsage().getUsed()      <=> runtime.totalMemory() - runtime.freeMemory()
memoryMxBean.getHeapMemoryUsage().getCommitted() <=> runtime.totalMemory()
memoryMxBean.getHeapMemoryUsage().getMax()       <=> runtime.maxMemory()

With the exception of the additional non-heap memory usage information provided by MemoryMXBean, are there any reasons why I should prefer it over Runtime, or vice-versa?


回答1:


There are none. JMX bean can be accessed externally and is meant for management tools like hyperic ( or even nagios ) - It would delegate to Runtime anyway.



来源:https://stackoverflow.com/questions/10509942/getting-memory-statistics-in-java-runtime-vs-memorymxbean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!