java获取服务器cpu ,内存,磁盘信息

此方法查询不需要集成别的jar包,减少项目依赖
public  void main1() {

        // 虚拟机级内存情况查询
        long vmFree = 0;
        long vmUse = 0;
        long vmTotal = 0;
        long vmMax = 0;
        int byteToMb = 1024 * 1024;
        Runtime rt = Runtime.getRuntime();
        vmTotal = rt.totalMemory() / byteToMb;
        vmFree = rt.freeMemory() / byteToMb;
        vmMax = rt.maxMemory() / byteToMb;
        vmUse = vmTotal - vmFree;
        System.out.println("JVM内存已用的空间为:" + vmUse + " MB");
        System.out.println("JVM内存的空闲空间为:" + vmFree + " MB");
        System.out.println("JVM总内存空间为:" + vmTotal + " MB");
        System.out.println("JVM总内存空间为:" + vmMax + " MB");

        System.out.println("======================================");
        // 操作系统级内存情况查询
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        String os = System.getProperty("os.name");
        long physicalFree = osmxb.getFreePhysicalMemorySize() / byteToMb;
        long physicalTotal = osmxb.getTotalPhysicalMemorySize() / byteToMb;
        long physicalUse = physicalTotal - physicalFree;
        System.out.println("操作系统的版本:" + os);
        System.out.println("操作系统物理内存已用的空间为:" + physicalFree + " MB");
        System.out.println("操作系统物理内存的空闲空间为:" + physicalUse + " MB");
        System.out.println("操作系统总物理内存:" + physicalTotal + " MB");

        // 获得线程总数
        ThreadGroup parentThread;
        int totalThread = 0;
        for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
                .getParent() != null; parentThread = parentThread.getParent()) {
            totalThread = parentThread.activeCount();
        }
        System.out.println("获得线程总数:" + totalThread);
    }
    public static void main(String[] args)
    {
        //获取内存
        getMemInfo();
        //获取cpu
        getCpuInfo();
        //获取磁盘
        getDiskInfo();
    }

    public static void getDiskInfo()
    {
        File[] disks = File.listRoots();
        for(File file : disks)
        {
            System.out.print(file.getPath() + "    ");
            System.out.print("空闲未使用 = " + file.getFreeSpace() / 1024 / 1024 / 1024+ "G" + "    ");// 空闲空间
            System.out.print("已经使用 = " + file.getUsableSpace() / 1024 / 1024 / 1024+ "G" + "    ");// 可用空间
            System.out.print("总容量 = " + file.getTotalSpace() / 1024 / 1024 / 1024+ "G" + "    ");// 总空间
            System.out.println();
        }
    }

    public static void getMemInfo()
    {
        OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        System.out.println("Total RAM:" + mem.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024 + "GB");
        System.out.println("Available RAM:" + mem.getFreePhysicalMemorySize() / 1024 / 1024 / 1024 + "GB");
    }

    public static void getCpuInfo()
    {

        OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double processCpuLoad = mem.getProcessCpuLoad();
        double systemCpuLoad = mem.getSystemCpuLoad();
        long processCpuTime = mem.getProcessCpuTime();
        System.out.println("cpu1:"+processCpuLoad);
        System.out.println("cpu2:"+systemCpuLoad);
        System.out.println("cpu3:"+processCpuTime);
    }

你可能感兴趣的:(java获取服务器cpu ,内存,磁盘信息)