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

Java MemoryPoolMXBean.getUsage()相对于Runtime.freeMemory()的优势,java,jmx,Java,Jmx,我有一个需求,需要使用收集JVM在固定时间间隔内的统计信息。稍后,我需要记录JVM可用内存/可用内存、已用内存、总内存 以下是我想到的两个选择 getMemoryPoolMXBeans()并遍历每个MemoryPoolMXBean,最后使用MemoryPoolMXBean.getUsage()收集统计信息 Runtime.freeMemory()、Runtime.maxMemory()、Runtime.totalMemory 哪种方法是收集统计数据的最佳方法。使用MemoryPoolMXBean

我有一个需求,需要使用收集JVM在固定时间间隔内的统计信息。稍后,我需要记录JVM可用内存/可用内存、已用内存、总内存

以下是我想到的两个选择

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

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

  • 哪种方法是收集统计数据的最佳方法。使用MemoryPoolMXBean.getUsage()或运行时类。

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

    如中所述,由于您可以向
    MemoryMXBean
    中添加通知侦听器,因此您有一种交互式的内存监控方式

    因此,对于局部检查,运行时可能就足够了,对于全局监视,
    MemoryMXBeans
    是一种方法