javacpu使用率
發布時間: 2023-07-14 06:49:21
A. java程序CPU佔用過高怎麼解決
Java程序就是這樣的,我去年做了一個桌面軟體,也出現這樣的問題,後來不用java的自動垃圾回收,在必要的時候手動調用System.gc(),這種情況就很少出現了,但是偶爾也還有。另外,CPU佔用率過高的情況一般是編程的問題,重點考慮線程資源共享和網路流處理兩個方面,java大部分的高CPU佔用率都跟這兩點有關系。
B. 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+"%";
}
}
熱點內容