MemoryPoolMXBean.getUsage() 相对于 Runtime.freeMemory() 的优点
我需要定期收集 JVM 的统计信息。 稍后,我需要记录 JVM 可用内存/空闲内存、已用内存、总内存。
以下是我想到的两个选择。
ManagementFactory.getMemoryPoolMXBeans() 并迭代每个 MemoryPoolMXBean,最后使用 MemoryPoolMXBean.getUsage() 收集统计信息。
Runtime.freeMemory(), Runtime.maxMemory(), Runtime.totalMemory
这是收集统计信息的最佳方法。 使用 MemoryPoolMXBean.getUsage() 或运行时类。
I have a requirement to collect statistics of a JVM in regular time intervals using. Later, i need to log the JVM available memory/free memory, used memory, total memory.
Following are the two options i have thought of.
ManagementFactory.getMemoryPoolMXBeans() and iterating through each MemoryPoolMXBean and finally collecting the statistics using MemoryPoolMXBean.getUsage().
Runtime.freeMemory(), Runtime.maxMemory(), Runtime.totalMemory
Which is the best way to collect the statistics. using MemoryPoolMXBean.getUsage() or Runtime Class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是关于在整个应用程序生命周期中监听您的内存使用情况,而不是在应用程序中的各个位置查找信息。
正如 Java 中的内存通知中提到的,您有一种监视内存的交互式方式,因为您可以向
MemoryMXBean
添加通知侦听器。因此,对于非常准时的本地检查,运行时可能就足够了,对于全局监控,
MemoryMXBeans
是最佳选择。This is about listening to your memory usage during the all application lifecycle instead of poking for information at various places within your application.
As mentioned in Memory notifications in Java, you have an interactive way to monitor memory since you can add a notification Listener to your
MemoryMXBean
.So for punctial very local check, Runtime may be enough, for global monitoring,
MemoryMXBeans
is the way to go.