导航:首页 > 编程语言 > java查看cpu

java查看cpu

发布时间:2023-01-14 15:02:45

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

(){
try{
StringprocCmd=System.getenv("windir")+"\system32\wbem\wmic.exeprocessgetCaption,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){
longidletime=c1[0]-c0[0];
longbusytime=c1[1]-c0[1];
return"CPU使用率:"+Double.valueOf(PERCENT*(busytime)*1.0/(busytime+idletime)).intValue()+"%";
}else{
return"CPU使用率:"+0+"%";
}
}catch(Exceptionex){
ex.printStackTrace();
return"CPU使用率:"+0+"%";
}
}

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class T {

private static final int DRIVE_TYPE_HARD = 3;

private static final String CHARSET = "GBK";

private static final String CAP_LOAD_PERCENTAGE = "LoadPercentage";

private static final String CAP_CAPACITY = "Capacity";
private static final String CAP_CAPTION = "Caption";
private static final String CAP_DRIVE_LETTER = "DriveLetter";
private static final String CAP_DRIVE_TYPE = "DriveType";
private static final String CAP_FREE_SPACE = "FreeSpace";

private static final List<String> CAPS_VOLUME = Arrays.asList(CAP_CAPACITY,
CAP_CAPTION, CAP_DRIVE_LETTER, CAP_DRIVE_TYPE, CAP_FREE_SPACE);

private static final String CMD_CPU = "wmic cpu get " + CAP_LOAD_PERCENTAGE;
private static final String CMD_VOLUME = "wmic volume get " + CAP_CAPACITY
+ "," + CAP_CAPTION + "," + CAP_DRIVE_LETTER + "," + CAP_DRIVE_TYPE + ","
+ CAP_FREE_SPACE;

public static void main(String[] args) throws IOException {
printDiskUsages(getDiskUsages());
printCpuUsage(getCpuLoadPercentage());
}

private static void printDiskUsages(List<DiskUsage> diskUsages) {
for (DiskUsage diskUsage : diskUsages) {
System.out.printf("%s占用率:%.2f%%\n", diskUsage.caption, diskUsage.usage);
}
}

private static void printCpuUsage(double cpuLoadPercentage) {
System.out.printf("CPU占用率:%.2f%%\n", cpuLoadPercentage);
}

/**
* 取得 CPU 占用率。
*
* @return
* @throws IOException
*/
private static double getCpuLoadPercentage() throws IOException {
Process process = Runtime.getRuntime().exec(CMD_CPU);
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET));
br.readLine(); // 舍弃标题行
br.readLine(); // 舍弃标题行下空行
String percentageLine = br.readLine();
if (percentageLine == null) {
return 0;
}
return Double.parseDouble(percentageLine.trim());
}

/**
* 取得硬盘占用率。
*
* @return
* @throws IOException
*/
private static List<DiskUsage> getDiskUsages() throws IOException {
Process process = Runtime.getRuntime().exec(CMD_VOLUME);
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET));
String captionLine = br.readLine(); // 舍弃标题行
br.readLine(); // 舍弃标题行下空行
Map<String, Integer> captionToIndex = parseVolumeCaptionLine(captionLine);
String line;
List<DiskUsage> result = new ArrayList<DiskUsage>();
while ((line = br.readLine()) != null) {
if (line.length() == 0) {
// 舍弃空行
continue;
}
DiskUsage diskUsage = parseVolumeLine(line, captionToIndex);
if (diskUsage != null) {
result.add(diskUsage);
}
}
Collections.sort(result, new DiskUsageComparator());
return result;
}

private static Map<String, Integer> parseVolumeCaptionLine(String captionLine) {
Map<String, Integer> captionToIndex = new HashMap<String, Integer>();
for (String caption : CAPS_VOLUME) {
captionToIndex.put(caption, captionLine.indexOf(caption));
}
return captionToIndex;
}

private static DiskUsage parseVolumeLine(String line,
Map<String, Integer> captionToIndex) {
int driveType = parseVolumeDriveType(line, captionToIndex);
if (driveType != DRIVE_TYPE_HARD) {
return null;
}
String driveLetter = parseVolumeDriveLetter(line, captionToIndex);
if (!isValidDriveLetter(driveLetter)) {
return null;
}

String caption = parseVolumeCaption(line, captionToIndex);
long capacity = parseVolumeCapacity(line, captionToIndex);
long freeSpace = parseVolumeFreeSpace(line, captionToIndex);
return new DiskUsage(caption,
((capacity - freeSpace) / (double) capacity) * 100);
}

private static boolean isValidDriveLetter(String driveLetter) {
if (driveLetter.length() != 2) {
return false;
}
return Character.isUpperCase(driveLetter.charAt(0));
}

private static int parseVolumeDriveType(String line,
Map<String, Integer> captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_DRIVE_TYPE),
captionToIndex.get(CAP_FREE_SPACE));
return Integer.parseInt(str.trim());
}

private static String parseVolumeDriveLetter(String line,
Map<String, Integer> captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_DRIVE_LETTER),
captionToIndex.get(CAP_DRIVE_TYPE));
return str.trim();
}

private static String parseVolumeCaption(String line,
Map<String, Integer> captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_CAPTION),
captionToIndex.get(CAP_DRIVE_LETTER));
return str.trim();
}

private static long parseVolumeCapacity(String line,
Map<String, Integer> captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_CAPACITY),
captionToIndex.get(CAP_CAPTION));
return Long.parseLong(str.trim());
}

private static long parseVolumeFreeSpace(String line,
Map<String, Integer> captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_FREE_SPACE));
return Long.parseLong(str.trim());
}

private static class DiskUsageComparator implements Comparator<DiskUsage> {

@Override
public int compare(DiskUsage o1, DiskUsage o2) {
return o1.caption.compareTo(o2.caption);
}

}

private static class DiskUsage {
public String caption;
public double usage;

public DiskUsage(String caption, Double usage) {
this.caption = caption;
this.usage = usage;
}
}
}

❸ 怎么用JAVA实现监控linux下CPU的使用率 windows下怎么查看呢用什么方法 请高手指教,谢谢!

用java的话,有两个方法:
1.利用java直接调用shell命令查看cpu的参数(系统不同命令也不同)
类似代码:
可以查考http://aimer311.javaeye.com/blog/347908
2.利用软件linux下可以安装net-snmp实现远程和本地监控
具体方法的话比较负责你网上查查

因为不知道你的linux到底是什么系统所有我没法给你写命令

❹ java windows怎么查看线程占用cpu

1、首先mp出该进程的所有线程及状态使用命令jstackPID命令打印出CPU占用过高进程的线程栈.jstack-l5683>5683.stack将进程id为5683的线程栈输出到了文件2、使用top命令找到耗cpu的线程使用top-H-pPID命令查看对应进程是哪个线程占

❺ 怎么查看java程序运行的峰值内存消耗(含虚拟机)和CPU消耗(ms)

查看java程序运行的峰值内存消耗(含虚拟机)和CPU消耗(ms)的方法:

  1. 用jdk自带的工具,jconsole.exe;

  2. 令行输入jconsole.exe;

  3. 就会出现一个window;

  4. 根据进程号选择要监控的虚拟机;

  5. 里面有内存、线程、包括各种对象定义占有的内存,都可以看到。

❻ Java怎么远程读取Linux的cpu使用率

linux获取cpu使用率
Windows查看CPU使用率很简单,我们通过任务管理器就能看到。那么对于linux来说,怎么查看获取CPU使用率呢?咗嚛本经验以Centos系统为例
工具/原料
Centos
获取CPU使用率
实时CPU使用率
类似任务管理器实时系统信息可以通过top命令查看。显示的信息四个参数分别是:用户的模式(user)、低优先级的用户模式(nice)、系统内核模式(system)以及系统空闲的处理器时间(idle)
查看CPU处理器使用率
对于CPU使用率一般都是通过CPU使用情况,查看/proc/stat
cpu状态文件
平均CPU使用率
对于一般某时间段CPU的使用率来说,可以通过查看/pRoc/loadavg
文件信息
第三方监控软件查看
网上有很多网管,监控软件安装配置好之后。可以通过网页管理查看CPU等硬件情况和CPU使用率,负载等参数
其它相关信息
内存使用率
查看
/proc/meminfo查看内存详细信息,也可以通过free
命令查看
网络利用率
通过查看文件/proc/net/dev
可以了解,centos系统的网络使用情况跟windows的网络情况类似
注意事项
如果是查看系统负载的话是需要通过,CPU使用率,内存使用率,网络负载,硬盘容量等等来综合计算出来的。如果对于linux不是特别了解,或者想一次获取比较全面,可以通过编写脚本或者相关的监控工具。

❼ java如何获取系统内存、cpu等信息。

亲.java的目录下有一个demo文件夹,里面有很多范例,其中就有读取cpu信息,望采纳点赞谢谢

❽ LINUX系统下查看JAVA的哪个线程占用CPU高

1、查看物理CPU的个数[root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
1
2、查看逻辑CPU的个数
[root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "processor"|wc -l
4
3、查看CPU是几核(即,核心数)
[root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "cores"|uniq
cpu cores : 4
4、查看CPU的主频
[root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep MHz|uniq
cpu MHz : 2499.982
5、当前操作系统内核信息
[root@MysqlCluster01 ~]# uname -a
Linux MysqlCluster01 2.6.32-431.20.3.el6.x86_64 #1 SMP Thu Jun 19 21:14:45 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
6、当前操作系统发行版信息
[root@MysqlCluster01 ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
7、内存使用情况
[root@MysqlCluster01 ~]# free -m
total used free shared buffers cached
Mem: 7863 2738 5125 0 141 835
-/+ buffers/cache: 1761 6102
Swap: 3967 0 3967

❾ Java如何读取CPU的数据信息

java获取所有系统信息(CPU、内存、进程等)的代码:
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

import mytools.com.sun.management.OperatingSystemMXBean;
import mytools.java.io.File;
import mytools.java.lang.management.ManagementFactory;
/**
* 获取windows系统信息(CPU,内存,文件系统)
* @author libing
*
*/

public class WindowsInfoUtil {
private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;

public static void main(String[] args) {
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}

//获取内存使用率
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="内存已使用:"+compare.intValue()+"%";
return str;
}

//获取文件系统使用率
public static List<String> getDisk() {
// 操作系统
List<String> list=new ArrayList<String>();
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
if(win.exists()){
long total=(long)win.getTotalSpace();
long free=(long)win.getFreeSpace();
Double compare=(Double)(1-free*1.0/total)*100;
String str=c+":盘 已使用 "+compare.intValue()+"%";
list.add(str);
}
}
return list;
}

//获得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+"%";
}
}

//读取cpu相关信息
private static 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 =substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process") || caption.equals("System")) {
if (s1.length() > 0)
idletime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() > 0)
kneltime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
usertime += Long.valueOf(s2).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;
}

/**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
* @param src 要截取的字符串
* @param start_idx 开始坐标(包括该坐标)
* @param end_idx 截止坐标(包括该坐标)
* @return
*/
private 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;
}
}

❿ 查看Java哪个线程占用CPU资源

以下方法在LINUX下执行通过:
1.先定位占用cpu高的进程
top
2.使用以下命令
ps p 14766 -L -o pcpu,pid,tid,time,tname,stat,psr | sort -n -k1 -r
其中14766是刚才1中cpu占用率高的进程pid
3.2.4 32525 32537 01:58:41 ? Sl 6
0.8 32525 1771 00:43:12 ? Sl 0
0.8 32525 1769 00:39:46 ? Sl 0
0.7 32525 12324 00:33:36 ? Sl 0
0.5 32525 1772 00:27:50 ? Sl 0
0.5 32525 1768 00:25:45 ? Sl 0
0.4 32525 30760 00:19:13 ? Sl 0
0.4 32525 1773 00:22:36 ? Sl 0
0.4 32525 1770 00:20:25 ? Sl 0
0.3 32525 32385 00:00:10 ? Sl 0
0.1 32525 31668 00:00:03 ? Sl 0
0.1 32525 31667 00:00:03 ? Sl 0
0.1 32525 1790 00:07:10 ? Sl 1
其中第3个结果就是此进程中有问题的线程nid
4.通过jstack命令mp出堆栈
"AppController_ThreadPool_L2_Pool Thread" daemon prio=10 tid=0x0000000051c2b000 nid=0x7bb3 in Object.wait() [0x000000005e3c5000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.company.threadpool.ThreadPoolImpl$PoolThread.run(ThreadPoolImpl.java:142)
- locked <0x00002aaca30341a8> (a org.company.threadpool.ThreadPoolImpl$PoolThread)
其中的nid就是线程的编码,只不过是经过了16进制的转换。
即十进制的31776对应的十六进制)0x7bb3,定位到线程后一切好办。

阅读全文

与java查看cpu相关的资料

热点内容
扣扣加密技巧 浏览:720
苹果如何创建服务器错误 浏览:495
软考初级程序员大题分值 浏览:473
js压缩视频文件 浏览:578
linux如何通过命令创建文件 浏览:989
应用加密app还能访问应用嘛 浏览:433
安卓怎么用支付宝交违章罚款 浏览:665
php面向对象的程序设计 浏览:504
数据挖掘算法书籍推荐 浏览:894
投诉联通用什么app 浏览:150
web服务器变更ip地址 浏览:954
java正则表达式验证邮箱 浏览:360
成熟商务男装下载什么软件app 浏览:609
加密2h代表长度是多少厘米 浏览:23
拍卖程序员 浏览:103
电脑的图片放在哪个文件夹 浏览:276
unsignedintjava 浏览:218
编译器下载地址 浏览:43
什么是面对对象编程 浏览:710
b站服务器什么时候恢复 浏览:722