共计 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 网 – 提供最优质的资源集合!
正文完