【问题标题】:Advantages of MemoryPoolMXBean.getUsage() over Runtime.freeMemory()MemoryPoolMXBean.getUsage() 相对于 Runtime.freeMemory() 的优势
【发布时间】:2009-05-12 11:53:35
【问题描述】:

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

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

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

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

这是收集统计信息的最佳方式。使用 MemoryPoolMXBean.getUsage() 或运行时类。

【问题讨论】:

    标签: java jmx


    【解决方案1】:

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

    Memory notifications in Java 中所述,您可以通过交互式 方式来监控内存,因为您可以将通知侦听器添加到您的MemoryMXBean

    所以对于准时的非常本地的检查,Runtime 可能就足够了,对于全局监控,MemoryMXBeans 是要走的路。

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2010-12-23
      • 2015-07-23
      • 2011-02-26
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多