java oshi如何查看cpu信息
oshi查看cpu信息
oshi可以跨平台查看服务器信息,其中cpu负载信息为当前占用cpu的时间。需要在一段时间内获取两次,然后相减得出这段时间内所占用的时间。这段时间除以总占用时间就是占用百分比。
pom引入依赖
<dependency> <groupid>com.github.oshi</groupid> <artifactid>oshi-core</artifactid> <version>3.12.2</version> </dependency>
测试类
package io.greatcolin.jvmmessage; import oshi.systeminfo; import oshi.hardware.centralprocessor; import oshi.hardware.globalmemory; import java.text.decimalformat; import java.util.properties; import java.util.concurrent.timeunit; /** * @author colin.cheng * @version v1.0 * @date created in 16:04 2019/8/16 */ public class oshitest { public static void main(string[] args) { while (true){ try { oshitest.printlncpuinfo(); oshitest.meminfo(); oshitest.getthread(); oshitest.setsysinfo(); oshitest.setjvminfo(); timeunit.seconds.sleep(5); }catch (exception e){ e.printstacktrace(); } } } private static void printlncpuinfo() throws interruptedexception { //system.out.println("----------------cpu信息----------------"); systeminfo systeminfo = new systeminfo(); centralprocessor processor = systeminfo.gethardware().getprocessor(); long[] prevticks = processor.getsystemcpuloadticks(); // 睡眠1s timeunit.seconds.sleep(1); long[] ticks = processor.getsystemcpuloadticks(); long nice = ticks[centralprocessor.ticktype.nice.getindex()] - prevticks[centralprocessor.ticktype.nice.getindex()]; long irq = ticks[centralprocessor.ticktype.irq.getindex()] - prevticks[centralprocessor.ticktype.irq.getindex()]; long softirq = ticks[centralprocessor.ticktype.softirq.getindex()] - prevticks[centralprocessor.ticktype.softirq.getindex()]; long steal = ticks[centralprocessor.ticktype.steal.getindex()] - prevticks[centralprocessor.ticktype.steal.getindex()]; long csys = ticks[centralprocessor.ticktype.system.getindex()] - prevticks[centralprocessor.ticktype.system.getindex()]; long user = ticks[centralprocessor.ticktype.user.getindex()] - prevticks[centralprocessor.ticktype.user.getindex()]; long iowait = ticks[centralprocessor.ticktype.iowait.getindex()] - prevticks[centralprocessor.ticktype.iowait.getindex()]; long idle = ticks[centralprocessor.ticktype.idle.getindex()] - prevticks[centralprocessor.ticktype.idle.getindex()]; long totalcpu = user + nice + csys + idle + iowait + irq + softirq + steal; system.out.println("----------------cpu信息----------------"); system.out.println("cpu核数:" + processor.getlogicalprocessorcount()); system.out.println("cpu系统使用率:" + new decimalformat("#.##%").format(csys * 1.0 / totalcpu)); system.out.println("cpu用户使用率:" + new decimalformat("#.##%").format(user * 1.0 / totalcpu)); system.out.println("cpu当前等待率:" + new decimalformat("#.##%").format(iowait * 1.0 / totalcpu)); system.out.println("cpu当前使用率:" + new decimalformat("#.##%").format(1.0-(idle * 1.0 / totalcpu))); } public static void meminfo(){ system.out.println("----------------主机内存信息----------------"); systeminfo systeminfo = new systeminfo(); globalmemory memory = systeminfo.gethardware().getmemory(); //总内存 long totalbyte = memory.gettotal(); //剩余 long acaliablebyte = memory.getavailable(); system.out.println("总内存 = " + formatbyte(totalbyte)); system.out.println("使用" + formatbyte(totalbyte-acaliablebyte)); system.out.println("剩余内存 = " + formatbyte(acaliablebyte)); system.out.println("使用率:" + new decimalformat("#.##%").format((totalbyte-acaliablebyte)*1.0/totalbyte)); } public static void setsysinfo(){ system.out.println("----------------操作系统信息----------------"); properties props = system.getproperties(); //系统名称 string osname = props.getproperty("os.name"); //架构名称 string osarch = props.getproperty("os.arch"); system.out.println("操作系统名 = " + osname); system.out.println("系统架构 = " + osarch); } public static void setjvminfo(){ system.out.println("----------------jvm信息----------------"); properties props = system.getproperties(); runtime runtime = runtime.getruntime(); //jvm总内存 long jvmtotalmemorybyte = runtime.totalmemory(); //jvm最大可申请 long jvmmaxmorybyte = runtime.maxmemory(); //空闲空间 long freememorybyte = runtime.freememory(); //jdk版本 string jdkversion = props.getproperty("java.version"); //jdk路径 string jdkhome = props.getproperty("java.home"); system.out.println("jvm内存总量 = " + formatbyte(jvmtotalmemorybyte)); system.out.println("jvm已使用内存 = " + formatbyte(jvmtotalmemorybyte-freememorybyte)); system.out.println("jvm剩余内存 = " + formatbyte(freememorybyte)); system.out.println("jvm内存使用率 = " + new decimalformat("#.##%").format((jvmtotalmemorybyte-freememorybyte)*1.0/jvmtotalmemorybyte)); system.out.println("java版本 = " + jdkversion); //system.out.println("jdkhome = " + jdkhome); } public static void getthread(){ system.out.println("----------------线程信息----------------"); threadgroup currentgroup =thread.currentthread().getthreadgroup(); while (currentgroup.getparent()!=null){ // 返回此线程组的父线程组 currentgroup=currentgroup.getparent(); } //此线程组中活动线程的估计数 int nothreads = currentgroup.activecount(); thread[] lstthreads = new thread[nothreads]; //把对此线程组中的所有活动子组的引用复制到指定数组中。 currentgroup.enumerate(lstthreads); for (thread thread : lstthreads) { system.out.println("线程数量:"+nothreads+" 线程id:" + thread.getid() + " 线程名称:" + thread.getname() + " 线程状态:" + thread.getstate()); } } public static string formatbyte(long bytenumber){ //换算单位 double format = 1024.0; double kbnumber = bytenumber/format; if(kbnumber<format){ return new decimalformat("#.##kb").format(kbnumber); } double mbnumber = kbnumber/format; if(mbnumber<format){ return new decimalformat("#.##mb").format(mbnumber); } double gbnumber = mbnumber/format; if(gbnumber<format){ return new decimalformat("#.##gb").format(gbnumber); } double tbnumber = gbnumber/format; return new decimalformat("#.##tb").format(tbnumber); } }
输出结果
# 没添加slf4j的依赖,不影响
slf4j: failed to load class "org.slf4j.impl.staticloggerbinder".
slf4j: defaulting to no-operation (nop) logger implementation
slf4j: see http://www.slf4j.org/codes.html#staticloggerbinder for further details.
----------------cpu信息----------------
cpu核数:4
cpu系统使用率:1.88%
cpu用户使用率:2.73%
cpu当前等待率:0%
cpu当前使用率:4.71%
----------------主机内存信息----------------
总内存 = 7.88gb
使用5.89gb
剩余内存 = 1.99gb
使用率:74.72%
----------------线程信息----------------
线程数量:5 线程id:2 线程名称:reference handler 线程状态:waiting
线程数量:5 线程id:3 线程名称:finalizer 线程状态:waiting
线程数量:5 线程id:4 线程名称:signal dispatcher 线程状态:runnable
线程数量:5 线程id:5 线程名称:attach listener 线程状态:runnable
线程数量:5 线程id:1 线程名称:main 线程状态:runnable
----------------操作系统信息----------------
操作系统名 = windows 7
系统架构 = amd64
----------------jvm信息----------------
jvm内存总量 = 123mb
jvm已使用内存 = 20.46mb
jvm剩余内存 = 102.54mb
jvm内存使用率 = 16.64%
java版本 = 1.8.0_65
oshi获取cpu/内存使用率前十的进程
cpu
@override public void getfirstcpuused() { operatingsystem windowsoperatingsystem = new windowsoperatingsystem(); list<osprocess> processlist = windowsoperatingsystem.getprocesses(10, operatingsystem.processsort.cpu); for (osprocess process : processlist) { //进程名,进程id,进程cpu使用率 system.out.println(string.format("name:%s pid: %d cpu:%.3f", process.getname(),process.getprocessid(), process.getprocesscpuloadcumulative())); } }
内存
public void getfirstmemused(){ operatingsystem windowsoperatingsystem = new windowsoperatingsystem(); list<osprocess> processlist = windowsoperatingsystem.getprocesses(10, operatingsystem.processsort.memory); for (osprocess process : processlist) { //进程名,京城id,进程cpu使用率 system.out.println(string.format("name:%s pid: %d cpu:%.3f", process.getname(),process.getprocessid(), process.getprocesscpuloadcumulative())); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。