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

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


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


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

最佳答案

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

Memory notifications in Java中所述,由于可以在MemoryMXBean中添加通知侦听器,因此您可以采用交互方式监视内存。

因此,对于标点非常本地的检查,运行时可能已足够;对于全局监视,MemoryMXBeans是必经之路。

09-12 11:42