简体   繁体   English

如何从代码中获取java中物理和虚拟内存的当前使用情况?

[英]How get current usage of physical and virtual memory in java from code?

我需要使用java代码获取当前在我的计算机上使用的物理和虚拟内存的百分比。

Not sure what you wanna accomplish with that but, take a look at MemoryMXBean and MemoryPoolMXBeans 不知道你想用它做什么,但是,看看MemoryMXBeanMemoryPoolMXBeans

You can do things like: 你可以这样做:

OperatingSystemMXBean osMBean = ManagementFactory.getOperatingSystemMXBean();

RuntimeMXBean runtimeMBean = ManagementFactory.getRuntimeMXBean();

System.out.println("Total physical memory:" + osMBean.getTotalPhysicalMemorySize() / 1024 + "kB");
System.out.println("Free physical memory:" + osMBean.getFreePhysicalMemorySize() / 1024 + "kB" );
System.out.println("Comm. virtual memory:" + osMBean.getProcessCpuTime() / 1024 + "kB" );
System.out.println("Total swap space:" + osMBean.getTotalSwapSpaceSize() / 1024 +" kB" );
System.out.println("Free swap space:" + osMBean.getFreeSwapSpaceSize() / 1024 +" kB" );
System.out.println("Operating system:" + osMBean.getName());
System.out.println("Arch:" + osMBean.getArch());
System.out.println("Processors:" + osMBean.getAvailableProcessors());
System.out.println("Process CPU time:" + osMBean.getProcessCpuTime() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM