java不用jni,也可以获得当前系统性能信息
最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接库了,但后来发现可以像下面这样做,不去调用jni,这样省去了很多看新技术的时间o(∩_∩)o...
在Java中,可以获得总的物理内存、剩余的物理内存、已使用的物理内存等信息,下面例子可以取得这些信息,并且获得在Windows下的内存使用率。
首先编写一个MonitorInfoBean类,用来装载监控的一些信息,包括物理内存、剩余的物理内存、已使用的物理内存、内存使用率等字段,该类的代码如下:
package com.amgkaka.performance; /** *//**
* 监视信息的JavaBean类.
* @author amg
* @version 1.0
* Creation date: 2008-4-25 - 上午10:37:00
*/
public class MonitorInfoBean {
/** *//** 可使用内存. */
private long totalMemory; /** *//** 剩余内存. */
private long freeMemory; /** *//** 最大可使用内存. */
private long maxMemory; /** *//** 操作系统. */
private String osName; /** *//** 总的物理内存. */
private long totalMemorySize; /** *//** 剩余的物理内存. */
private long freePhysicalMemorySize; /** *//** 已使用的物理内存. */
private long usedMemory; /** *//** 线程总数. */
private int totalThread; /** *//** cpu使用率. */
private double cpuRatio; public long getFreeMemory() {
return freeMemory;
} public void setFreeMemory(long freeMemory) {
this.freeMemory = freeMemory;
} public long getFreePhysicalMemorySize() {
return freePhysicalMemorySize;
} public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
this.freePhysicalMemorySize = freePhysicalMemorySize;
} public long getMaxMemory() {
return maxMemory;
} public void setMaxMemory(long maxMemory) {
this.maxMemory = maxMemory;
} public String getOsName() {
return osName;
} public void setOsName(String osName) {
this.osName = osName;
} public long getTotalMemory() {
return totalMemory;
} public void setTotalMemory(long totalMemory) {
this.totalMemory = totalMemory;
} public long getTotalMemorySize() {
return totalMemorySize;
} public void setTotalMemorySize(long totalMemorySize) {
this.totalMemorySize = totalMemorySize;
} public int getTotalThread() {
return totalThread;
} public void setTotalThread(int totalThread) {
this.totalThread = totalThread;
} public long getUsedMemory() {
return usedMemory;
} public void setUsedMemory(long usedMemory) {
this.usedMemory = usedMemory;
} public double getCpuRatio() {
return cpuRatio;
} public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}
接着编写一个获得当前的监控信息的接口,该类的代码如下所示:
package com.amgkaka.performance; /** *//**
* 获取系统信息的业务逻辑类接口.
* @author amg * @version 1.0
* Creation date: 2008-3-11 - 上午10:06:06
*/
public interface IMonitorService {
/** *//**
* 获得当前的监控对象.
* @return 返回构造好的监控对象
* @throws Exception
* @author amgkaka
* Creation date: 2008-4-25 - 上午10:45:08
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception; }
该类的实现类MonitorServiceImpl如下所示:
package com.amgkaka.performance; import java.io.InputStreamReader;
import java.io.LineNumberReader; import sun.management.ManagementFactory; import com.sun.management.OperatingSystemMXBean; /** *//**
* 获取系统信息的业务逻辑实现类.
* @author amg * @version 1.0 Creation date: 2008-3-11 - 上午10:06:06
*/
public class MonitorServiceImpl implements IMonitorService {
//可以设置长些,防止读到运行此次系统检查时的cpu占用率,就不准了
private static final int CPUTIME = 5000; private static final int PERCENT = 100; private static final int FAULTLENGTH = 10; /** *//**
* 获得当前的监控对象.
* @return 返回构造好的监控对象
* @throws Exception
* @author amg * Creation date: 2008-4-25 - 上午10:45:08
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024; // 可使用内存
long totalMemory = Runtime.getRuntime().totalMemory() / kb;
// 剩余内存
long freeMemory = Runtime.getRuntime().freeMemory() / kb;
// 最大可使用内存
long maxMemory = Runtime.getRuntime().maxMemory() / kb; OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean(); // 操作系统
String osName = System.getProperty("os.name");
// 总的物理内存
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
// 已使用的物理内存
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
.getFreePhysicalMemorySize())
/ kb; // 获得线程总数
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent())
;
int totalThread = parentThread.activeCount(); double cpuRatio = 0;
if (osName.toLowerCase().startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
} // 构造返回对象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreeMemory(freeMemory);
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
infoBean.setMaxMemory(maxMemory);
infoBean.setOsName(osName);
infoBean.setTotalMemory(totalMemory);
infoBean.setTotalMemorySize(totalMemorySize);
infoBean.setTotalThread(totalThread);
infoBean.setUsedMemory(usedMemory);
infoBean.setCpuRatio(cpuRatio);
return infoBean;
} /** *//**
* 获得CPU使用率.
* @return 返回cpu使用率
* @author amg * Creation date: 2008-4-25 - 下午06:05:11
*/
private double getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir")
+ "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
+ "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return Double.valueOf(
PERCENT * (busytime) / (busytime + idletime))
.doubleValue();
} else {
return 0.0;
}
} catch (Exception ex) {
ex.printStackTrace();
return 0.0;
}
} /** *//**
* 读取CPU信息.
* @param proc
* @return
* @author amg * Creation date: 2008-4-25 - 下午06:10:14
*/
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = Bytes.substring(line, capidx, cmdidx - 1)
.trim();
String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
// log.info("line="+line);
if (caption.equals("System Idle Process")
|| caption.equals("System")) {
idletime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
idletime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
continue;
} kneltime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
usertime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
} /** *//**
* 测试方法.
* @param args
* @throws Exception
* @author amg * Creation date: 2008-4-30 - 下午04:47:29
*/
public static void main(String[] args) throws Exception {
IMonitorService service = new MonitorServiceImpl();
MonitorInfoBean monitorInfo = service.getMonitorInfoBean();
System.out.println("cpu占有率=" + monitorInfo.getCpuRatio()); System.out.println("可使用内存=" + monitorInfo.getTotalMemory());
System.out.println("剩余内存=" + monitorInfo.getFreeMemory());
System.out.println("最大可使用内存=" + monitorInfo.getMaxMemory()); System.out.println("操作系统=" + monitorInfo.getOsName());
System.out.println("总的物理内存=" + monitorInfo.getTotalMemorySize() + "kb");
System.out.println("剩余的物理内存=" + monitorInfo.getFreeMemory() + "kb");
System.out.println("已使用的物理内存=" + monitorInfo.getUsedMemory() + "kb");
System.out.println("线程总数=" + monitorInfo.getTotalThread() + "kb");
}
}
该实现类中需要用到一个自己编写byte的工具类,该类的代码如下所示:
package com.amgkaka.performance; /** *//**
* byte操作类.
* @author amg * @version 1.0
* Creation date: 2008-4-30 - 下午04:57:23
*/
public class Bytes {
/** *//**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在
* 包含汉字的字符串时存在隐患,现调整如下:
* @param src 要截取的字符串
* @param start_idx 开始坐标(包括该坐标)
* @param end_idx 截止坐标(包括该坐标)
* @return
*/
public static String substring(String src, int start_idx, int end_idx){
byte[] b = src.getBytes();
String tgt = "";
for(int i=start_idx; i<=end_idx; i++){
tgt +=(char)b[i];
}
return tgt;
}
}
运行下MonitorBeanImpl类,读者将会看到当前的内存、cpu利用率等信息。
(转自:http://kakaluyi.iteye.com/blog/211492)
java不用jni,也可以获得当前系统性能信息的更多相关文章
- ubuntu下Java通过JNI调用C
下面看一个实例,如下: public class TestJNI { static { System.loadLibrary("diaoyong"); // 程序在加载时,自动加载 ...
- JAVA使用JNI调用C++动态链接库
JAVA使用JNI调用C++动态链接库 使用JNI连接DLL动态链接库,并调用其中的函数 首先 C++中写好相关函数,文件名为test.cpp,使用g++编译为DLL文件,指令如下: g++ -sha ...
- 【Java的JNI快速学习教程】
1. JNI简介 JNI是Java Native Interface的英文缩写,意为Java本地接口. 问题来源:由于Java编写底层的应用较难实现,在一些实时性要求非常高的部分Java较难胜任(实时 ...
- java通过jni方式获取硬盘序列号(windows,linux)
linux系统java通过jni方式获取硬盘序列号 http://blog.csdn.net/starter110/article/details/8186788 使用jni在windows下读取硬盘 ...
- JAVA中JNI的简单使用
了解JNI:JAVA因其跨平台特性而受人们喜爱,也正因此,使得它和本机各种内部联系变得很少,所以JNI(Java Native Interface)就是用来解决JAVA本地操作的一种方式.JAVA通过 ...
- Java通过JNI调用C/C++
From:http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html JNI是JAVA标准平台中的一个重要功能,它弥补了JAVA的与 ...
- java通过JNI接口调用C语言-初级
JNI(java native interface):即java本地调用C的接口. 先看整体运行: 下面是过程: #vim test.java public class test{ public na ...
- Java通过JNI调用dll详细过程(转)
源:Java通过JNI调用dll详细过程 最近项目有这样一个需求,在已有的CS软件中添加一个链接,将当前登录用户的用户名加密后放在url地址中,在BS的login方法里通过解密判断,如果为合法用户则无 ...
- Java之JNI的介绍与应用20170622
/*************************************************************************************************** ...
随机推荐
- 细微之处:比较两种CSS清除浮动的兼容
http://www.cnblogs.com/bienfantaisie/archive/2011/05/27/2059597.html 清除浮动是连续浮动元素之后的必备工作,在工作中我做到需要清除浮 ...
- RPM软件包管理的查询功能
以后大家升级rpm包的时候,不要用Uvh了! 我推荐用Fvh 前者会把没有安装过得包也给装上,后者只会更新已经安装的包 总结:未安装的加上小写p,已安装的不需要加p 查询q rpm {- ...
- linux 下查看某个端口是否被占用
lsof -i:端口号 转自: http://my.oschina.net/u/193184/blog/146885
- 【Linux】为啥查某个进程的线程,查出来的所有线程的pid不一样啊
楼上说的linux线程和进程是一样的,这个说法是错误的. 看了楼主的问题,感觉楼主是被PID给弄混了,线程进程都会有自己的ID,这个ID就叫做PID,PID是不特指进程ID,线程ID也可以叫做PID. ...
- EtherCAT数据帧结构
EtherCAT数据直接使用以太网数据帧(以太网帧解释http://blog.chinaunix.net/uid-23080322-id-118440.html)传输,使用的帧类型为0x88A4.Et ...
- POJ 3083
---恢复内容开始--- http://poj.org/problem?id=3083 题目大意就是给你要你从S走到E,且只有.代表的地方才是可以走的,有三种方式的走法. 一.是向左优先转,从S到E的 ...
- wx.html2.WebView在 target="_blank" or rel="external" 没有反映的解决方法
在wx.html2.EVT_WEBVIEW_LOADED中,用WebView.RunScript运行删除链接目标的脚本 javaScriptStr = '''function deleteBlank( ...
- typedef和#define
typedef:在计算机编程语言中用来为复杂的声明定义简单的别名(给类型起别名,整体类型替换),它本身是一种存储类的关键字,与auto.extern.mutable.static.register等关 ...
- 解决TIME_WAIT过多造成的问题
sh-4.1# netstat -an |awk '/tcp/ {++S[$NF]}END {for (a in S) print a , S[a]}' TIME_WAIT CLOSE_WAIT ES ...
- ASP.NET之AreaRegistration
ASP.NETMVC的AreaRegistration是用来干什么呢? 它是用来提供一个很好的接口让我们可以将Controller定义在其他的Library项目中,这主要可以用来解决模块化开发:一般情 ...