【问题标题】:Getting Memory Statistics in Java: Runtime vs. MemoryMXBean在 Java 中获取内存统计信息:运行时与 MemoryMXBean
【发布时间】:2012-05-17 14:31:52
【问题描述】:

我正在创建一个预定的执行程序来读取 JVM 的内存使用情况。我遇到了两种在正在运行的 JVM 中获取内存统计信息的方法 - Runtime 和 MemoryMXBean,它们的方法之间有以下对应关系:

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

除了 MemoryMXBean 提供的额外的非堆内存使用信息之外,还有什么理由让我更喜欢它而不是运行时,反之亦然?

【问题讨论】:

    标签: java memory-profiling


    【解决方案1】:

    没有。 JMX bean 可以从外部访问,适用于管理工具,例如 hyperic (甚至 nagios ) - 无论如何它都会委托给 Runtime。

    【讨论】:

    • 是的。两种方式都为已用内存返回完全相同的值。
    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    相关资源
    最近更新 更多