java怎么读取jvm内存

91次阅读
没有评论

共计 1013 个字符,预计需要花费 3 分钟才能阅读完成。

在 Java 中,可以使用 ManagementFactory 类来读取 JVM 内存信息。下面是一个示例代码:

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;

public class JVMMemoryReader {public static void main(String[] args) {MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();

        System.out.println("Heap Memory Usage:");
        System.out.println("   Init: " + heapMemoryUsage.getInit());
        System.out.println("   Used: " + heapMemoryUsage.getUsed());
        System.out.println("   Committed: " + heapMemoryUsage.getCommitted());
        System.out.println("   Max: " + heapMemoryUsage.getMax());

        System.out.println("Non-Heap Memory Usage:");
        System.out.println("   Init: " + nonHeapMemoryUsage.getInit());
        System.out.println("   Used: " + nonHeapMemoryUsage.getUsed());
        System.out.println("   Committed: " + nonHeapMemoryUsage.getCommitted());
        System.out.println("   Max: " + nonHeapMemoryUsage.getMax());
    }
}

运行上述代码会输出 JVM 的堆内存和非堆内存的使用情况。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-09发表,共计1013字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)