关于java测cpu的信息

博主:adminadmin 2023-01-14 18:24:08 402

今天给各位分享java测cpu的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 如何查看服务器的CPU使用率

public static String 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 "CPU使用率:"+Double.valueOf(PERCENT * (busytime)*1.0 / (busytime + idletime)).intValue()+"%"; 

             } else { 

                 return "CPU使用率:"+0+"%"; 

             } 

         } catch (Exception ex) { 

             ex.printStackTrace(); 

             return "CPU使用率:"+0+"%"; 

         } 

     }

请问怎么测试一个java程序占用的内存和cpu消耗?

只能查看JAVA 虚拟机占用的内存Runtime.getRuntime().maxMemory() 返回 Java 虚拟机试图使用的最大内存量。Runtime.getRuntime().freeMemory() 返回 Java 虚拟机中的空闲内存量。Runtime.getRuntime().totalMemory() 返回 Java 虚拟机中的内存总量。

java怎样获取CPU占用率和硬盘占用率

通过jmx可以监控vm内存使用,系统内存使用等,以下是网上某博客代码,特点是通过window和linux命令获得CPU使用率。

利用java程序实现获取计算机cpu利用率和内存使用信息。

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;   

    }   

}

JAVA代码如何测linux下的CPU使用率?

如果是远程的linux系统,可以使用java调用Telnet工具类执行远程脚本。代码参考如下:package test;

import java.io.InputStream;

import java.io.PrintStream;

import org.apache.commons.net.telnet.TelnetClient;

public class Shell

{

private TelnetClient telnet = new TelnetClient();

private InputStream in;

private PrintStream out;

private char prompt = ';// 普通用户结束

public Shell(String ip, int port, String user, String password)

{

try

{

telnet.connect(ip, port);

in = telnet.getInputStream();

out = new PrintStream(telnet.getOutputStream());

// 根据root用户设置结束符

this.prompt = user.equals("root") ? '#' : '';

login(user, password);

}

catch (Exception e)

{

e.printStackTrace();

}

}

/**

* 登录

*

* @param user

* @param password

*/

public void login(String user, String password)

{

// readUntil("login:");

readUntil("login:");

write(user);

readUntil("Password:");

write(password);

readUntil(prompt + "");

}

/**

* 读取分析结果

*

* @param pattern

* @return

*/

public String readUntil(String pattern)

{

try

{

char lastChar = pattern.charAt(pattern.length() - 1);

StringBuffer sb = new StringBuffer();

char ch = (char)in.read();

while (true)

{

sb.append(ch);

if (ch == lastChar)

{

if (sb.toString().endsWith(pattern))

{

return sb.toString();

}

}

ch = (char)in.read();

System.out.print(ch);

}

}

catch (Exception e)

{

e.printStackTrace();

}

return null;

}

/**

* 写操作

*

* @param value

*/

public void write(String value)

{

try

{

out.println(value);

out.flush();

}

catch (Exception e)

{

e.printStackTrace();

}

}

/**

* 向目标发送命令字符串

*

* @param command

* @return

*/

public String sendCommand(String command)

{

try

{

write(command);

return readUntil(prompt + "");

}

catch (Exception e)

{

e.printStackTrace();

}

return null;

}

/**

* 关闭连接

*/

public void disconnect()

{

try

{

telnet.disconnect();

}

catch (Exception e)

{

e.printStackTrace();

}

}

public static void main(String[] args) {

TelnetClient telnet = new TelnetClient();

try {

Shell she =new Shell("IP地址", 23, "用户名", "密码");

System.out.println(she);

System.out.println(she.sendCommand("ls"));

she.disconnect();

}catch (Exception e) {

// TODO: handle exception

}

}

}代码是参考的网上源码,使用时将System.out.println(she.sendCommand("ls"));中的ls命令换成查看CPU使用率的命令,例如top,vmstat,mpstat,prstat -J或者prstat -T 等命令。根据其结果做解析即可。

如何测量java程序的cpu占用情况

在JDK的bin目录有一个工具,叫jconsole.exe,双击打开后,连接你需要监控的JVM,然后就可以来查看CPU、内存占用情况。

还有JDK提供的 jvisualvm.exe,功能更强大。

关于java测cpu和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。