❶ 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)的方法:
用jdk自帶的工具,jconsole.exe;
令行輸入jconsole.exe;
就會出現一個window;
根據進程號選擇要監控的虛擬機;
裡面有內存、線程、包括各種對象定義佔有的內存,都可以看到。
❻ 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,定位到線程後一切好辦。