Java JMX MemoryMXBean

JMX Java About 1,595 words

功能

MemoryMXBean可以获取Java服务的内存信息:

  • HeapMemoryUsage: 堆内存使用情况
  • NonHeapMemoryUsage: 非堆内存使用情况
  • ObjectPendingFinalizationCount: 等待回收的对象数量

代码

public class MemoryMXBeanTest {

    public static void main(String[] args) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        long heapMemoryInit = heapMemoryUsage.getInit();
        long heapMemoryMax = heapMemoryUsage.getMax();
        long heapMemoryUsed = heapMemoryUsage.getUsed();
        long heapMemoryCommitted = heapMemoryUsage.getCommitted();
        System.out.println("Heap Memory Usage: " + heapMemoryUsage.toString());

        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        long nonHeapMemoryUsageInit = nonHeapMemoryUsage.getInit();
        long nonHeapMemoryUsageMax = nonHeapMemoryUsage.getMax();
        long nonHeapMemoryUsageUsed = nonHeapMemoryUsage.getUsed();
        long nonHeapMemoryUsageCommitted = nonHeapMemoryUsage.getCommitted();
        System.out.println("Non Heap Memory Usage: " + nonHeapMemoryUsage.toString());

        int objectPendingFinalizationCount = memoryMXBean.getObjectPendingFinalizationCount();
        System.out.println("objectPendingFinalizationCount: " + objectPendingFinalizationCount);

    }

}

输出

Heap Memory Usage: init = 536870912(524288K) used = 2097152(2048K) committed = 536870912(524288K) max = 8589934592(8388608K)
Non Heap Memory Usage: init = 7667712(7488K) used = 9912352(9680K) committed = 16318464(15936K) max = -1(-1K)
objectPendingFinalizationCount: 0
Views: 728 · Posted: 2024-01-03

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh