java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core

Posted 至高吴上(Alfa.wu)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core相关的知识,希望对你有一定的参考价值。

 

maven 引用

<dependency>
    <groupId>com.github.dblock</groupId>
    <artifactId>oshi-core</artifactId>
    <version>LATEST</version>
</dependency>

取得 cpu信息 示例

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;

public class ComputerIdentifier
{
    public static String generateLicenseKey() throws Exception
    {
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
        HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
        CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();

        String vendor = operatingSystem.getManufacturer();
        String processorSerialNumber = centralProcessor.getSystemSerialNumber();
        String processorIdentifier = centralProcessor.getIdentifier();
        int processors = centralProcessor.getLogicalProcessorCount();

        String delimiter = "#";

        return vendor +
                delimiter +
                processorSerialNumber +
                delimiter +
                processorIdentifier +
                delimiter +
                processors;
    }

    public static void main(String[] arguments) throws Exception
    {
        String identifier = generateLicenseKey();
        System.out.println(identifier);
    }
}

 

获取jvm数据

jvm数据是监控应用很重要的一系列参数,一般本地开发的时候可以通过jconsole来连到对应的进程上面,查看相关指标数据,但是在线上环境就不适合通过jconsole来查看了, 所以我们现在使用通过java代码来获得数据,然后上报出去,然后在外部通过展示。

那么如何通过java代码来得到这些参数呢?

获得jvm的堆内存代码

MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
System.out.println("jvm.heap.init is " + (heapMemoryUsage.getInit()));
System.out.println("jvm.heap.used is " + (heapMemoryUsage.getUsed()));
System.out.println("jvm.heap.committed is " + (heapMemoryUsage.getCommitted()));
System.out.println("jvm.heap.max is " + (heapMemoryUsage.getMax()));

获得jvm的非堆内存代码

MemoryUsage nonHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
System.out.println("jvm.nonheap.init is " + (nonHeapMemoryUsage.getInit()));
System.out.println("jvm.nonheap.used is " + (nonHeapMemoryUsage.getUsed()));
System.out.println("jvm.nonheap.committed is " + (nonHeapMemoryUsage.getCommitted()));
System.out.println("jvm.nonheap.max is " + (nonHeapMemoryUsage.getMax()));

上面的方法只能得到jvm的堆和非堆的整体数据,一般都知道堆和非堆里面都几个不同的区,用来做不同功能,那么如何得到不同区的数据呢?不多说,上代码

for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {  
final String kind = pool.getType() == MemoryType.HEAP ? "heap" : "nonheap";  
final MemoryUsage usage = pool.getUsage();  
System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".init is " + usage.getInit());  
System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".used is " + usage.getUsed());  
System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName()+ ".committed is " + usage.getCommitted());  
System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".max is " + usage.getMax()); 
}

这样就可以得到各种区的具体参数。

 

以上是关于java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core的主要内容,如果未能解决你的问题,请参考以下文章

JVM 及 垃圾回收机制原理

java 获取系统信息及CPU的使用率(转)

在JAVA或JSP中,能否取得登录WINDOWS的域名及用户名?

Java入门——Runtime类

Linux如何查看系统及硬件信息

JavaScript获取客户端计算机硬件及系统等信息的方法