CPU and memory usage statistics in Java applicationHello therei have finished a application, now i need to get this application 's CPU and memory usage while it is executing.lots of tools i found are all presentations of the usage, showing you a graph. but i really want is get these data dynamicly, say eg: every milli-second, i want to get these data in a time series.i am new to JNI, so, is anyone had the experience with this? you got any idea i could follow, what should i do i steps??
or if you know any free jar package can finish the task i could just simply import to my project?many many thanks for your advice and help in advance. 
我需要动态获取一个程序执行期间cpu及内存的利用率,很多找到的工具都只是展示利用率的数据, 我希望实现的是获取这些利用率得值,如每隔10毫秒读一次, 以组成一个时间序列。不知道谁有这方面的经验??   请教一下应该如何去实现?? 或者是否知道有什么现成的jar包可以利用??万分感谢  

解决方案 »

  1.   

    没用过看起来挺深的。获取当前虚拟机的内存使用状况的话,这个好不好用?
    java.lang.Runtime.totalMemory()-java.lang.Runtime.freeMemory()
      

  2.   

    1. 使用JNI, 使用操作系统api读取
    2. 使用命令行, 捕获命令行文本去分析(如网络连接 netstat -an, 系统进程: wmic process)
    3. 通过SQLServer数据库. 利用sql语法去读取这些网上都有, google一下好了
      

  3.   

    import org.apache.commons.modeler.Registry;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;import javax.management.MBeanServer;
    import javax.management.ObjectName;public class RuntimeInfoAccessorBean {    private Log logger = LogFactory.getLog(RuntimeInfoAccessorBean.class);    public RuntimeInformation getRuntimeInformation() throws Exception {
            MBeanServer mBeanServer = new Registry().getMBeanServer();
            RuntimeInformation ri = new RuntimeInformation();        try {
                ObjectName runtimeOName = new ObjectName("java.lang:type=Runtime");
                ri.setStartTime(JmxTools.getLongAttr(mBeanServer, runtimeOName, "StartTime"));
                ri.setUptime(JmxTools.getLongAttr(mBeanServer, runtimeOName, "Uptime"));
                ri.setVmVendor(JmxTools.getStringAttr(mBeanServer,runtimeOName,"VmVendor"));            ObjectName osOName = new ObjectName("java.lang:type=OperatingSystem");
                ri.setOsName(JmxTools.getStringAttr(mBeanServer, osOName, "Name"));
                ri.setOsVersion(JmxTools.getStringAttr(mBeanServer, osOName, "Version"));            if(!ri.getVmVendor().startsWith("IBM Corporation")){
                    ri.setTotalPhysicalMemorySize(JmxTools.getLongAttr(mBeanServer, osOName, "TotalPhysicalMemorySize"));
                    ri.setCommittedVirtualMemorySize(JmxTools.getLongAttr(mBeanServer, osOName, "CommittedVirtualMemorySize"));
                    ri.setFreePhysicalMemorySize(JmxTools.getLongAttr(mBeanServer, osOName, "FreePhysicalMemorySize"));
                    ri.setFreeSwapSpaceSize(JmxTools.getLongAttr(mBeanServer, osOName, "FreeSwapSpaceSize"));
                    ri.setTotalSwapSpaceSize(JmxTools.getLongAttr(mBeanServer, osOName, "TotalSwapSpaceSize"));
                    ri.setProcessCpuTime(JmxTools.getLongAttr(mBeanServer, osOName, "ProcessCpuTime"));
                } else {
                    ri.setTotalPhysicalMemorySize(JmxTools.getLongAttr(mBeanServer, osOName, "TotalPhysicalMemory"));
                }            return ri;
            } catch (Exception e) {
                logger.debug("OS information is unavailable");
                return null;
            }
        }
    }//------------------------------------------------------------------------------------------
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;import javax.management.*;
    import javax.management.openmbean.CompositeData;public class JmxTools {    private static Log logger = LogFactory.getLog(JmxTools.class);    public static Object getAttribute(MBeanServer mBeanServer, ObjectName oName, String attrName) throws Exception {
            try {
                return mBeanServer.getAttribute(oName, attrName);
            } catch (AttributeNotFoundException e) {
                logger.error(oName + " does not have \""+attrName+"\" attribute");
                return null;
            }
        }    public static long getLongAttr(MBeanServer mBeanServer, ObjectName oName, String attrName, long defaultValue) {
            try {
                Object o = mBeanServer.getAttribute(oName, attrName);
                return o == null ? defaultValue : ((Long) o).longValue();
            } catch (Exception e) {
                return defaultValue;
            }
        }    public static long getLongAttr(MBeanServer mBeanServer, ObjectName oName, String attrName) throws Exception {
            return ((Long) mBeanServer.getAttribute(oName, attrName)).longValue();
        }    public static int getIntAttr(MBeanServer mBeanServer, ObjectName oName, String attrName) throws Exception {
            return ((Integer) mBeanServer.getAttribute(oName, attrName)).intValue();
        }    public static String getStringAttr(MBeanServer mBeanServer, ObjectName oName, String attrName) throws Exception {
            Object o = getAttribute(mBeanServer, oName, attrName);
            return o == null ? null : o.toString();
        }    public static long getLongAttr(CompositeData cds, String name) {
            Object o = cds.get(name);
            if (o != null && o instanceof Long) {
                return ((Long)o).longValue();
            } else {
                return 0;
            }
        }    public static String getStringAttr(CompositeData cds, String name) {
            Object o = cds.get(name);
            return o != null ? o.toString() : null;
        }    public static boolean getBooleanAttr(CompositeData cds, String name) {
            Object o = cds.get(name);        if (o != null && o instanceof Boolean) {
                return ((Boolean)o).booleanValue();
            } else {
                return false;
            }
        }    public static int getIntAttr(CompositeData cds, String name, int defaultValue) {
            Object o = cds.get(name);        if (o != null && o instanceof Integer) {
                return ((Integer)o).intValue();
            } else {
                return defaultValue;
            }
        }    public static boolean hasAttribute(MBeanServer server, ObjectName mbean, String attrName) throws Exception {
            MBeanInfo info = server.getMBeanInfo(mbean);
            MBeanAttributeInfo[] ai = info.getAttributes();
            for (int i = 0; i < ai.length; i++) {
                if (ai[i].getName().equals(attrName)) {
                    return true;
                }
            }
            return false;
        }
    }RuntimeInformation只是一个普通的bean,用来存放信息的。定时调用getRuntimeInformation方法即可获得cpu和内存等详细信息。特别注意在启动服务器的jvm参数中加上-Dcom.sun.management.jmxremote
    否则无法获取这些信息。
      

  4.   

    用SNMP协议是很容易就实现了的
      

  5.   

    SNMP有这相关方面的定义??simple network management protocol....
      

  6.   

    JDK的DEMO里不是有很多范例么!
      

  7.   

    windows下,java本事根本就做不到每10毫秒读取一次,不用jni是不可能办到的
      

  8.   

    进你JDK这个目录看jdk1.5.0_12\demo\management\MemoryMonitor
      

  9.   


    这个还真有哇。。
    我一直以为java运行在jvm上读取不了硬件