【java之获取cpu/内存信息】 -买球官网平台

`
gaojingsong
  • 浏览: 1030951 次
  • 性别:
  • 来自: 深圳
博主相关
  • 博客
  • 微博
  • 相册
  • 收藏
  • 文章分类
    社区版块
    • ( 4)
    • ( 0)
    • ( 80)
    存档分类
    最新评论

    【java之获取cpu/内存信息】

    代码

    import org.junit.test;
    import org.slf4j.logger;
    import org.slf4j.loggerfactory;
    import oshi.systeminfo;
    import oshi.hardware.centralprocessor;
    import oshi.hardware.centralprocessor.ticktype;
    import oshi.hardware.globalmemory;
    import oshi.hardware.hardwareabstractionlayer;
    import oshi.util.formatutil;
    import oshi.util.util;
    import java.util.arrays;
    /**
     * os information test
     */
    public class oshitest {
        private static logger logger = loggerfactory.getlogger(oshitest.class);
        @test
    public void test() {
            systeminfo si = new systeminfo();
            hardwareabstractionlayer hal = si.gethardware();
            logger.info("checking memory...");
            printmemory(hal.getmemory());
            logger.info("checking cpu...");
            printcpu(hal.getprocessor());
        }
        private static void printmemory(globalmemory memory) {
            logger.info("memory avail:{} mb" , memory.getavailable() / 1024 / 1024 );//memory avail:6863 mb
    logger.info("memory total:{} mb" , memory.gettotal() / 1024 / 1024 );//memory total:16384 mb
    }
        private static void printcpu(centralprocessor processor) {
            logger.info(string.format("cpu load: %.1f%% (os mxbean)%n", processor.getsystemcpuload() * 100));//cpu load: 24.9% (os mxbean)
    logger.info("cpu load averages : {}", processor.getsystemloadaverage());//cpu load averages : 1.5234375
    logger.info("uptime: "   formatutil.formatelapsedsecs(processor.getsystemuptime()));
            logger.info("context switches/interrupts: "   processor.getcontextswitches()   " / "   processor.getinterrupts());
            long[] prevticks = processor.getsystemcpuloadticks();
            logger.info("cpu, iowait, and irq ticks @ 0 sec:"   arrays.tostring(prevticks));
             //wait a second...
    util.sleep(1000);
            long[] ticks = processor.getsystemcpuloadticks();
            logger.info("cpu, iowait, and irq ticks @ 1 sec:"   arrays.tostring(ticks));
            long user = ticks[ticktype.user.getindex()] - prevticks[ticktype.user.getindex()];
            long nice = ticks[ticktype.nice.getindex()] - prevticks[ticktype.nice.getindex()];
            long sys = ticks[ticktype.system.getindex()] - prevticks[ticktype.system.getindex()];
            long idle = ticks[ticktype.idle.getindex()] - prevticks[ticktype.idle.getindex()];
            long iowait = ticks[ticktype.iowait.getindex()] - prevticks[ticktype.iowait.getindex()];
            long irq = ticks[ticktype.irq.getindex()] - prevticks[ticktype.irq.getindex()];
            long softirq = ticks[ticktype.softirq.getindex()] - prevticks[ticktype.softirq.getindex()];
            long steal = ticks[ticktype.steal.getindex()] - prevticks[ticktype.steal.getindex()];
            long totalcpu = user   nice   sys   idle   iowait   irq   softirq   steal;
            logger.info(string.format(
                    "user: %.1f%% nice: %.1f%% system: %.1f%% idle: %.1f%% iowait: %.1f%% irq: %.1f%% softirq: %.1f%% steal: %.1f%%%n",
                    100d * user / totalcpu, 100d * nice / totalcpu, 100d * sys / totalcpu, 100d * idle / totalcpu,
                    100d * iowait / totalcpu, 100d * irq / totalcpu, 100d * softirq / totalcpu, 100d * steal / totalcpu));
            logger.info(string.format("cpu load: %.1f%% (counting ticks)%n", processor.getsystemcpuloadbetweenticks() * 100));
            double[] loadaverage = processor.getsystemloadaverage(3);
            logger.info("cpu load averages:"   (loadaverage[0] < 0 ? " n/a" : string.format(" %.2f", loadaverage[0]))
                      (loadaverage[1] < 0 ? " n/a" : string.format(" %.2f", loadaverage[1]))
                      (loadaverage[2] < 0 ? " n/a" : string.format(" %.2f", loadaverage[2])));
            // per core cpu
    stringbuilder proccpu = new stringbuilder("cpu load per processor:");
            double[] load = processor.getprocessorcpuloadbetweenticks();
            for (double avg : load) {
                proccpu.append(string.format(" %.1f%%", avg * 100));
            }
            logger.info(proccpu.tostring());
        }
    }

     

    结果

    16:03:08.573 [main] info org.apache.dolphinscheduler.common.os.oshitest - checking memory...

    16:03:10.592 [main] info org.apache.dolphinscheduler.common.os.oshitest - memory avail:155 mb

    16:03:10.594 [main] info org.apache.dolphinscheduler.common.os.oshitest - memory total:4095 mb

    16:03:10.594 [main] info org.apache.dolphinscheduler.common.os.oshitest - checking cpu...

    16:03:10.772 [main] debug oshi.hardware.common.abstractcentralprocessor - oracle mxbean detected.

    16:03:12.319 [main] debug oshi.hardware.platform.windows.windowscentralprocessor - initialized processor

    16:03:12.322 [main] info org.apache.dolphinscheduler.common.os.oshitest - cpu load: 41.0% (os mxbean)

     

    16:03:12.322 [main] info org.apache.dolphinscheduler.common.os.oshitest - cpu load averages : -1.0

    16:03:12.326 [main] info org.apache.dolphinscheduler.common.os.oshitest - uptime: 1 days, 20:39:54

    16:03:12.328 [main] info org.apache.dolphinscheduler.common.os.oshitest - context switches/interrupts: 1714488866 / 570600239

    16:03:12.330 [main] info org.apache.dolphinscheduler.common.os.oshitest - cpu, iowait, and irq ticks @ 0 sec:[37287359, 0, 19608289, 234918099, 0, 520263, 600050, 0]

    16:03:13.332 [main] info org.apache.dolphinscheduler.common.os.oshitest - cpu, iowait, and irq ticks @ 1 sec:[37287421, 0, 19608313, 234920002, 0, 520263, 600057, 0]

    16:03:13.333 [main] info org.apache.dolphinscheduler.common.os.oshitest - user: 3.1% nice: 0.0% system: 1.2% idle: 95.3% iowait: 0.0% irq: 0.0% softirq: 0.4% steal: 0.0%

     

    16:03:13.333 [main] info org.apache.dolphinscheduler.common.os.oshitest - cpu load: 19.8% (counting ticks)

     

    16:03:13.333 [main] info org.apache.dolphinscheduler.common.os.oshitest - cpu load averages: n/a n/a n/a

    16:03:13.337 [main] info org.apache.dolphinscheduler.common.os.oshitest - cpu load per processor: 21.3% 18.3%

     

     

     

    分享到:
    评论

    相关推荐

      通过java程序获取windows/linux系统的cpu信息、内存信息、硬盘信息、网络信息、操作系统信息等。

      利用 java 获取电脑的cpu信息、内存信息

      java获取计算机的cpu使用率和内存等一系列数据的时候,给人的第一感觉就是要用动态链接库,其实java也完全可以使用的,所以就简单的写了一下,基本信息都能获取的到,还就cpu使用率做了一个仪表图表

      该文档中详细描述了在java开发中如何获取系统中内存、cpu相关信息,非常实用。

      java 获取计算机cpu利用率和内存使用信息,需要的自己下载测试吧。

      java获取系统信息(cpu_内存_硬盘_进程等)的相关方法

      java获取系统信息(cpu,内存,硬盘)方法及jar包。还可以直接读出ip地址及使用的网卡。

      获取linux和windows上的cpu信息,有jar有dll

       前段时间摸索在java中怎么获取系统信息包括cpu、内存、硬盘信息等,刚开始使用java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插件进行获取。下面列举出...

      #通过任务管理器查询pid #打印指定pid堆栈日志到指定文件 ...#在processexplorer上查看进程cpu和内存使用情况,找到java进程点开可以看tid线程使用情况,通过tid可在线程日志中找到指定线程进行分析

      主要介绍了java实现获取cpu、内存、硬盘、网络等信息的方法,涉及java使用第三方jar包针对本机硬件的cpu、内存、硬盘、网络信息等的读取相关操作技巧,需要的朋友可以参考下

      通过hyperic-hq产品的基础包sigar.jar来实现服务器状态数据的获取。sigar.jar包是通过本地方法来调用操作系统api来获取系统相关数据。windows操作系统下sigar.jar依赖sigar-amd64-winnt.dll或sigar-x86-winnt.dll,...

      java通过sigar获取进程的相关信息 ,包含各个进程的pid、名称、cpu使用率、所占内存大小等

      主要介绍了java如何获取系统cpu、内存占用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

      java获取系统信息(cpu,内存,硬盘,进程等)的相关方法.pdf

      appinfomanager.java获取软件信息和内存信息,其它的信息获取在infoactivity.java

      通过jmx可以监控vm内存使用,系统内存使用等 ,特点是通过window和linux命令获得cpu使用率。

      java ipmi,vxipmi 通过java获取服务器信息,温度、风扇、内存等

      用java取得linux系统cpu、内存的实时信息

    global site tag (gtag.js) - google analytics