Sigar(全称System Information Gatherer And Reporter,即系统信息收集报表器),它提供了一个开源的跨平台的收集计算机硬件和操作系统信息的API(该API底层接口用C语言编写),本文将演示如何借助Sigar API获取内存信息:
package com.ghj.packageoftest; import org.hyperic.sigar.Mem; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; import org.hyperic.sigar.Swap; /** * 借助Sigar API获取获取内存信息 * * @author GaoHuanjie */ public class MemoryTool { public static void main(String[] args) throws SigarException { Sigar sigar = new Sigar(); // 物理内存信息 Mem mem = sigar.getMem(); System.out.println("mem total:" + mem.getTotal() + " B"); System.out.println("mem ram:" + mem.getRam() + " B"); System.out.println("mem used:" + mem.getUsed() + " B"); System.out.println("mem free:" + mem.getFree() + " B"); System.out.println("mem actualUsed:" + mem.getActualUsed() + " B"); System.out.println("mem actualFree:" + mem.getActualFree() + " B"); System.out.println("mem usedPercent:" + mem.getUsedPercent() + "%"); System.out.println("mem freePercent:" + mem.getFreePercent() + "%"); // 交换区信息 Swap swap = sigar.getSwap(); System.err.println("swap total:" + swap.getTotal() + " B"); System.err.println("swap used:" + swap.getUsed() + " B"); System.err.println("swap free:" + swap.getFree() + " B"); System.err.println("swap pageIn:" + swap.getPageIn()); System.err.println("swap pageOut:" + swap.getPageOut()); } }
【0分下载工程代码】
时间: 2024-10-25 03:17:36