MemoryPoolMXBean.getUsage() 相对于 Runtime.freeMemory() 的优点

发布于 2024-07-19 22:11:43 字数 362 浏览 5 评论 0原文

我需要定期收集 JVM 的统计信息。 稍后,我需要记录 JVM 可用内存/空闲内存、已用内存、总内存。

以下是我想到的两个选择。

  1. ManagementFactory.getMemoryPoolMXBeans() 并迭代每个 MemoryPoolMXBean,最后使用 MemoryPoolMXBean.getUsage() 收集统计信息。

  2. 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.

  1. ManagementFactory.getMemoryPoolMXBeans() and iterating through each MemoryPoolMXBean and finally collecting the statistics using MemoryPoolMXBean.getUsage().

  2. Runtime.freeMemory(), Runtime.maxMemory(), Runtime.totalMemory

Which is the best way to collect the statistics. using MemoryPoolMXBean.getUsage() or Runtime Class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2024-07-26 22:11:43

这是关于在整个应用程序生命周期中监听您的内存使用情况,而不是在应用程序中的各个位置查找信息。

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文