利用JAVA代码获得各操作系统内存

各系统获取系统内存的指令
系统命令
windowwmic memorychip get capacity
linuxcat /proc/meminfo
macsysctl hw.memsize
FreeBSDsysctl hw.physmem

 

Java 获取系统内存

package com.bigdata.sampler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yuanhailong on 2019/8/29.
 */
public class Hardware {
    private static final Logger LOG = LoggerFactory.getLogger(Hardware.class);

    private static final String LINUX_MEMORY_INFO_PATH = "/proc/meminfo";

    private static final Pattern LINUX_MEMORY_REGEX = Pattern.compile("^MemTotal:\\s*(\\d+)\\s+kB$");


    //获取系统CPU数量
    public static int getNumberCPUCores() {
        return Runtime.getRuntime().availableProcessors();
    }

    /**
     * 获取系统内存
     * @return
     */
    public static long getSizeOfPhysicalMemory() {
        //在Oracle JVM下系统有可能直接告诉我们系统内存,因此首先通过这种方式尝试获取内存
        try {
            Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
            Method method = clazz.getMethod("getTotalPhysicalMemorySize");
            OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();

            //检查是否是sun提供的MXBean
            if (clazz.isInstance(operatingSystemMXBean)) {
                return (Long) method.invoke(operatingSystemMXBean);
            }
        }
        catch (Exception e) {
        }

        if(System.getProperty("os.name").toLowerCase().startsWith("linux")){  //linux system
            return getSizeOfPhysicalMemoryForLinux();
        }else if (System.getProperty("os.name").toLowerCase().startsWith("window")){
            return getSizeOfPhysicalMemoryForWindows();
        }else if (System.getProperty("os.name").toLowerCase().startsWith("mac")){
            return getSizeOfPhysicalMemoryForMac();
        }else if (System.getProperty("os.name").toLowerCase().startsWith("freebsd")){
            return getSizeOfPhysicalMemoryForFreeBSD();
        }else{
            return -1;  //如果都不是返回-1
        }



    }

    /**
     * Linux系统获取内存
     * @return
     */
    private static long getSizeOfPhysicalMemoryForLinux() {
        try (BufferedReader lineReader = new BufferedReader(new FileReader(LINUX_MEMORY_INFO_PATH))) {
            String line;
            while ((line = lineReader.readLine()) != null) {
                Matcher matcher = LINUX_MEMORY_REGEX.matcher(line);
                if (matcher.matches()) {
                    String totalMemory = matcher.group(1);
                    return Long.parseLong(totalMemory) * 1024L; // 换算为KB
                }
            }
            return -1;
        }
        catch (Exception e) {
            return -1;
        }
    }

    /**
     * Mac系统获取内存
     */
    private static long getSizeOfPhysicalMemoryForMac() {
        BufferedReader bi = null;
        try {
            Process proc = Runtime.getRuntime().exec("sysctl hw.memsize");
            bi = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line;
            while ((line = bi.readLine()) != null) {
                if (line.startsWith("hw.memsize")) {
                    long memsize = Long.parseLong(line.split(":")[1].trim());
                    bi.close();
                    proc.destroy();
                    return memsize;
                }
            }
        } catch (Throwable t) {
            return -1;
        } finally {
            if (bi != null) {
                try {
                    bi.close();
                } catch (IOException ignored) {}
            }
        }
        return -1;
    }

    /**
     * FreeBSD系统
     */
    private static long getSizeOfPhysicalMemoryForFreeBSD() {
        BufferedReader bi = null;
        try {
            Process proc = Runtime.getRuntime().exec("sysctl hw.physmem");

            bi = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            String line;
            while ((line = bi.readLine()) != null) {
                if (line.startsWith("hw.physmem")) {
                    long memsize = Long.parseLong(line.split(":")[1].trim());
                    bi.close();
                    proc.destroy();
                    return memsize;
                }
            }
            return -1;
        }
        catch (Throwable t) {
            return -1;
        }
        finally {
            if (bi != null) {
                try {
                    bi.close();
                } catch (IOException ignored) {}
            }
        }
    }

    /**
     * Window 系统
     */
    private static long getSizeOfPhysicalMemoryForWindows() {
        BufferedReader bi = null;
        try {
            Process proc = Runtime.getRuntime().exec("wmic memorychip get capacity");
            bi = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = bi.readLine();
            if (line == null) {
                return -1L;
            }
            if (!line.startsWith("Capacity")) {
                return -1L;
            }
            long sizeOfPhyiscalMemory = 0L;
            while ((line = bi.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }

                line = line.replaceAll(" ", "");
                sizeOfPhyiscalMemory += Long.parseLong(line);
            }
            return sizeOfPhyiscalMemory;
        }
        catch (Throwable t) {
            return -1L;
        }
        finally {
            if (bi != null) {
                try {
                    bi.close();
                } catch (Throwable ignored) {}
            }
        }
    }


    public static void main(String[] args) {
        System.out.println(Hardware.getSizeOfPhysicalMemory());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值