java获取服务器CPU,内存,硬盘使用量

2024-03-01 06:38

本文主要是介绍java获取服务器CPU,内存,硬盘使用量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1. /** 
  2.  * <获取系统信息类> 
  3.  * <获取系统信息,如CPU、内存、硬盘使用情况> 
  4.  * @author  wenkaixuan 
  5.  * @version  [版本号, 2012-5-9] 
  6.  * @see  [相关类/方法] 
  7.  * @since  [产品/模块版本] 
  8.  */  
  9. public class GetSystemInfo  
  10. {  
  11.     private static final int CPUTIME = 500;  
  12.       
  13.     private static final int PERCENT = 100;  
  14.       
  15.     private static final int FAULTLENGTH = 10;  
  16.       
  17.     //获取内存使用率  
  18.     public static String getMemery()  
  19.     {  
  20.         OperatingSystemMXBean osmxb = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();  
  21.         // 总的物理内存+虚拟内存  
  22.         long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();  
  23.         // 剩余的物理内存  
  24.         long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();  
  25.         Double compare = (Double)(1 - freePhysicalMemorySize * 1.0 / totalvirtualMemory) * 100;  
  26.         String str = "内存已使用:" + compare.intValue() + "%";  
  27.         return str;  
  28.     }  
  29.       
  30.     //获取文件系统使用率  
  31.     public static List<String> getDisk()  
  32.     {  
  33.         // 操作系统  
  34.         List<String> list = new ArrayList<String>();  
  35.         for (char c = 'A'; c <= 'Z'; c++)  
  36.         {  
  37.             String dirName = c + ":/";  
  38.             File win = new File(dirName);  
  39.             if (win.exists())  
  40.             {  
  41.                 long total = (long)win.getTotalSpace();  
  42.                 long free = (long)win.getFreeSpace();  
  43.                 Double compare = (Double)(1 - free * 1.0 / total) * 100;  
  44.                 String str = c + ":盘  已使用 " + compare.intValue() + "%";  
  45.                 list.add(str);  
  46.             }  
  47.         }  
  48.         return list;  
  49.     }  
  50.       
  51.     //获得cpu使用率  
  52.     public static String getCpuRatioForWindows()  
  53.     {  
  54.         try  
  55.         {  
  56.             String procCmd =  
  57.                 System.getenv("windir")  
  58.                     + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";  
  59.             // 取进程信息  
  60.             long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));  
  61.             Thread.sleep(CPUTIME);  
  62.             long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));  
  63.             if (c0 != null && c1 != null)  
  64.             {  
  65.                 long idletime = c1[0] - c0[0];  
  66.                 long busytime = c1[1] - c0[1];  
  67.                 return "CPU使用率:" + Double.valueOf(PERCENT * (busytime) * 1.0 / (busytime + idletime)).intValue() + "%";  
  68.             }  
  69.             else  
  70.             {  
  71.                 return "CPU使用率:" + 0 + "%";  
  72.             }  
  73.         }  
  74.         catch (Exception ex)  
  75.         {  
  76.             ex.printStackTrace();  
  77.             return "CPU使用率:" + 0 + "%";  
  78.         }  
  79.     }  
  80.       
  81.     //读取cpu相关信息  
  82.     private static long[] readCpu(final Process proc)  
  83.     {  
  84.         long[] retn = new long[2];  
  85.         try  
  86.         {  
  87.             proc.getOutputStream().close();  
  88.             InputStreamReader ir = new InputStreamReader(proc.getInputStream());  
  89.             LineNumberReader input = new LineNumberReader(ir);  
  90.             String line = input.readLine();  
  91.             if (line == null || line.length() < FAULTLENGTH)  
  92.             {  
  93.                 return null;  
  94.             }  
  95.             int capidx = line.indexOf("Caption");  
  96.             int cmdidx = line.indexOf("CommandLine");  
  97.             int rocidx = line.indexOf("ReadOperationCount");  
  98.             int umtidx = line.indexOf("UserModeTime");  
  99.             int kmtidx = line.indexOf("KernelModeTime");  
  100.             int wocidx = line.indexOf("WriteOperationCount");  
  101.             long idletime = 0;  
  102.             long kneltime = 0;  
  103.             long usertime = 0;  
  104.             while ((line = input.readLine()) != null)  
  105.             {  
  106.                 if (line.length() < wocidx)  
  107.                 {  
  108.                     continue;  
  109.                 }  
  110.                 // 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,  
  111.                 // ThreadCount,UserModeTime,WriteOperation  
  112.                 String caption = substring(line, capidx, cmdidx - 1).trim();  
  113.                 String cmd = substring(line, cmdidx, kmtidx - 1).trim();  
  114.                 if (cmd.indexOf("wmic.exe") >= 0)  
  115.                 {  
  116.                     continue;  
  117.                 }  
  118.                 String s1 = substring(line, kmtidx, rocidx - 1).trim();  
  119.                 String s2 = substring(line, umtidx, wocidx - 1).trim();  
  120.                 if (caption.equals("System Idle Process") || caption.equals("System"))  
  121.                 {  
  122.                     if (s1.length() > 0)  
  123.                         idletime += Long.valueOf(s1).longValue();  
  124.                     if (s2.length() > 0)  
  125.                         idletime += Long.valueOf(s2).longValue();  
  126.                     continue;  
  127.                 }  
  128.                 if (s1.length() > 0)  
  129.                     kneltime += Long.valueOf(s1).longValue();  
  130.                 if (s2.length() > 0)  
  131.                     usertime += Long.valueOf(s2).longValue();  
  132.             }  
  133.             retn[0] = idletime;  
  134.             retn[1] = kneltime + usertime;  
  135.             return retn;  
  136.         }  
  137.         catch (Exception ex)  
  138.         {  
  139.             ex.printStackTrace();  
  140.         }  
  141.         finally  
  142.         {  
  143.             try  
  144.             {  
  145.                 proc.getInputStream().close();  
  146.             }  
  147.             catch (Exception e)  
  148.             {  
  149.                 e.printStackTrace();  
  150.             }  
  151.         }  
  152.         return null;  
  153.     }  
  154.       
  155.     /** 
  156.     * 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下: 
  157.     * @param src 要截取的字符串 
  158.     * @param start_idx 开始坐标(包括该坐标) 
  159.     * @param end_idx 截止坐标(包括该坐标) 
  160.     * @return 
  161.     */  
  162.     private static String substring(String src, int start_idx, int end_idx)  
  163.     {  
  164.         byte[] b = src.getBytes();  
  165.         String tgt = "";  
  166.         for (int i = start_idx; i <= end_idx; i++)  
  167.         {  
  168.             tgt += (char)b[i];  
  169.         }  
  170.         return tgt;  
  171.     }  

这篇关于java获取服务器CPU,内存,硬盘使用量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/761515

相关文章

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads