を取得し、私は特定のプロセスのために、以下のデータを達成するための最善のアプローチを調査してきた:のJava(Windowsの場合) - プロセスID、メモリ使用率、ディスク使用量、ネットワーク使用率
- CPU使用率
- 私はOSHI(オペレーティングシステム&ハードウェアインフォアで行くことにしたメモリ使用量
- ディスク使用量
- ネットワークの使用状況
API)。 私にとっては不幸なことですが、このAPIは必要な情報を私に提供していません。プロセスあたりのCPU使用量などの計算方法に関する基本的な知識が必要です。
私の質問は、どのようにメモリ、ディスク、ネットワークの使用状況をプロセスIDで取得できますか?
claculator.exe実行中のプロセスの実際のCPU使用率を取得する:例えばprcoess
ごとのCPU使用データの次の例を用いて
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
public class processCPUusage {
public static void main(String[] args) throws InterruptedException {
OSProcess process;
long currentTime,previousTime = 0,timeDifference;
double cpu;
int pid = 7132;
SystemInfo si = new SystemInfo();
OperatingSystem os = si.getOperatingSystem();
CentralProcessor processor = si.getHardware().getProcessor();
int cpuNumber = processor.getLogicalProcessorCount();
boolean processExists = true;
while (processExists) {
process = os.getProcess(pid); // calculator.exe process id
if (process != null) {
// CPU
currentTime = process.getKernelTime() + process.getUserTime();
if (previousTime != -1) {
// If we have both a previous and a current time
// we can calculate the CPU usage
timeDifference = currentTime - previousTime;
cpu = (100d * (timeDifference/((double) 1000)))/cpuNumber;
System.out.println(cpu);
}
previousTime = currentTime;
Thread.sleep(1000);
} else {
processExists = false;
}
}
}
}
フレームワークを私は https://github.com/oshi/oshi/tree/master/src/site/markdown を使用すると、目的の機能が示されますが、適切な例はありません。
- 言語:Javaの8
- ビルド・ツール:Mavenの2
- OS:Windowsの10 *
OSHIライブラリ+ SLF4J
<dependency>
<groupId>com.github.dblock</groupId>
<artifactId>oshi-core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>