/**
* 获取当前程序已使用内存
* */
* 获取当前程序已使用内存
* */
public static long getSystemMemory(){
Runtime runtime = Runtime.getRuntime();
long total = runtime.totalMemory();//总内存
long free = runtime.freeMemory();//空闲内存
long use = total - free;//已用内存
return use;
}
//获取系统内存数据
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
OperatingSystemMXBean os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long physicalFree = os.getFreePhysicalMemorySize();//系统空闲内存
long physicalTotal = os.getTotalPhysicalMemorySize();//系统最大内存
long physicalUse = physicalTotal - physicalFree;//已使用总的物理内存
System.out.println(physicalUse / 1024 + "KB");