- 浏览: 407512 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (267)
- 序列号 (1)
- jquery (14)
- Eclipse插件 (3)
- Flex (2)
- Rose (1)
- Myeclipse (3)
- maven (4)
- SpringSecurity (2)
- sproutcore (1)
- Http (2)
- svn (4)
- gwt (3)
- jetty (1)
- freebsd (1)
- java (29)
- Oracle (21)
- windows (1)
- Mybatis3.0 (3)
- Struts2 (6)
- easyui (1)
- JPA (1)
- UML (1)
- dom4j (1)
- design pattern (2)
- JavaScript (8)
- FreeMarker (2)
- SQLServer (1)
- memcached (3)
- urlrewrite (1)
- MonggoDB (1)
- Solr (1)
- Lucene (1)
- SQL (2)
- JODConverter (1)
- xml (2)
- pdf2swf (1)
- flexpaper (1)
- android (2)
- jsp (5)
- mobile (1)
- weblogic (3)
- Tomcat (8)
- xss (1)
- db2 (2)
- webservice (1)
- spring (2)
- Gradle (1)
- linux (4)
- ldap (1)
- 软件工程 (1)
- 开发模型 (1)
- linux系统硬件配置查看方法 (1)
- cas (3)
- mysql (1)
- HAProxy (0)
- html (1)
- Web前端 (1)
最新评论
-
super_zou:
...
世界主要国家地区下拉菜单三级联动1 -
ldl_xz:
http://www.9958.pw/post/wenku 曾 ...
利用pdf2swf将PDF转换成SWF -
superlxw1234:
请教一下楼主,你们是如何解决使用MAgent时候,一台主Mem ...
memcached集群-magent -
头子:
中间那段关于“空隙”的解决方式很赞
HTML固定表头Table -
qiankun:
比较实用,好好学习了一下
Mybatis 在dao中获取分页的总记录数
java获得CPU使用率,系统内存,虚拟机内存等情况(不用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利用率等信息。
评论
2 楼
mouqj
2012-02-10
这个统计的应该是JAVA虚拟机的内存使用
1 楼
kimble
2011-12-15
我获取的结果是:
cpu占有率=1.0
可使用内存=5056
剩余内存=4503
最大可使用内存=65088
操作系统=Windows 2003
总的物理内存=2097151kb
剩余的物理内存=4503kb
已使用的物理内存=0kb
线程总数=5kb
怎么和我实际的,不吻合?
cpu占有率=1.0
可使用内存=5056
剩余内存=4503
最大可使用内存=65088
操作系统=Windows 2003
总的物理内存=2097151kb
剩余的物理内存=4503kb
已使用的物理内存=0kb
线程总数=5kb
怎么和我实际的,不吻合?
相关推荐
java获得CPU、内存使用率,通过jni获得C程序获得CPU、内存的使用率,可直接运行。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
用jni获得cpu和内存使用率的例子,给出了c++源码和java代码。
JNI攻略之十一――启动虚拟机调用java类
测量CPU和内存的占用率常常是检查Java应用程序是否达到特定性能的一个重要环节。一个主要的解决方案是使用操作系统自带的本机系统调用,将数据通过JNI(Java Native Interface,Java本机接口)传输给Java。与调用...
博文链接:https://huangpengxiao.iteye.com/blog/96670
Java_JNI_获得系统进程信息实例.doc
详细内容请看https://blog.csdn.net/qq_34950682/article/details/95790190
java获得CPU序列号,java通过jni连接c语言,从C中获得CPU的序列号。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
文档里描述了如何通过jni方法在java与c++代码之间传递非基本类型数据
我们上层应用平台是使用java开发的,但开放平台是使用c++开发,虽提供给我们对应的.h头文件,但在java中来解析这些数据就显然有些复杂,在实现、效率上都可能不太理想。 解决方案: 请C++同事帮忙写个dll程序,dll去...
安卓中实现jni读取内存信息源代码
java中RCP中使用JNI
使用Java的JNI技术来获取系统内存的信息
移植了LeakTracer库到安卓平台,可以直接使用,带有例子
Java中使用Jni简单示例过程.doc
但这样做通常会导致丧失平台可移植性,通常是在特定的需求下进行,例如使用旧的其他语言的库、需要获得Java类库不支持的某种基于具体平台的特性、大量数学计算性能优化等。希望本文档会给有需要的朋友带来帮助;
JNI攻略之十――操作Java虚拟机
一般来说,JNI有点复杂。但是,如果你仅仅单向调用--从Java调用本地代码,并且仅使用基本型进行通讯--事情还是很简单的。本文介绍了在Java中声明JNI方法,给出了在Java应用程序中使用JNI来监视CPU的实例。
该资源抱哈了一个用java实现的jni的基本实例,可以运行,包含了其中的cpp文件,简单易懂,喜欢的下载!
[JAVA]使用JNI技术实现JAVA程序调用dll、[JAVA]使用JNI技术实现JAVA程序调用dll