How do I monitor the computer's CPU, memory, and disk usage in Java?
Along the lines of what I mentioned in this post. I recommend you use the SIGAR API. I use the SIGAR API in one of my own applications and it is great. You'll find it is stable, well supported, and full of useful examples. It is open-source with a GPL 2 Apache 2.0 license. Check it out. I have a feeling it will meet your needs.
Using Java and the Sigar API you can get Memory, CPU, Disk, Load-Average, Network Interface info and metrics, Process Table information, Route info, etc.
Determining minimum memory requirement and CPU usage
You may take help of jvisualvm
path of this tool is Program Files\Java\jdk1.6.0_38\bin\jvisualvm.exe
You may use it to determine your cpu utilization and memory consumption.
As you can see your CPU utilization and memory utilization. Hope this tool may help you.
how to read the memory and cpu usage of a process started by java
Try this:
1) To hold all your running processes:
private List<String> processes = new ArrayList<String>();
2) To get all java running processes:
private void getRunningProcesses() throws Exception {
Process process = Runtime.getRuntime().exec("top -b -n1 -c");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ( (line = br.readLine()) != null) {
if( line.contains("java") ) processes.add( line );
}
}
3) To get an information line from PID ( getRunningProcesses() first ):
private String getByPid( int pid ) {
for ( String line : processes ) {
if ( line.startsWith( String.valueOf( pid ) ) ) {
return line;
}
}
return "";
}
4) Now you have a line with all "top" information from that PID. Get the CPU %:
private Double getCpuFromProcess( String process ) {
//PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
Double result = 0.0;
String[] items = process.replace(" "," ").replace(" "," ").split(" ");
result = Double.valueOf( items[8].replace(",", ".") );
return result;
}
Done.
EDIT: Don't forget to clear processes list before each call.
EDIT 2: Call
getRunningProcesses()
, then getCpuFromProcess( getByPid( the_pid ) )
How to generate CPU and memory usage graphs after load/soak test?
You can use JDK tool such as JvisualVM.
It can generate summary visualized report of cpu and memory situation
For more performance monitor and bottleneck location, you can taste SJK
For example:
> java -jar sjk.jar ttop -p 6344 -n 20 -o CPU
2013-09-09T11:32:45.426+0300 Process summary
process cpu=31.08%
application cpu=28.90% (user=6.40% sys=22.49%)
other: cpu=2.19%
heap allocation rate 5260kb/s
[000001] user= 3.12% sys=11.40% alloc= 762kb/s - main
[092018] user= 0.94% sys= 0.47% alloc= 335kb/s - RMI TCP Connection(16)-10.139.211.172
[092016] user= 0.31% sys= 1.56% alloc= 1927kb/s - SVN-WJGGZ
[092007] user= 0.78% sys= 8.75% alloc= 860kb/s - Worker-4863
[092012] user= 0.31% sys= 0.31% alloc= 429kb/s - Worker-4864
[091966] user= 0.16% sys= 0.00% alloc= 90kb/s - Worker-4859
[092022] user= 0.16% sys= 0.00% alloc= 6871b/s - JMX server connection timeout 92022
[000002] user= 0.00% sys= 0.00% alloc= 0b/s - Reference Handler
[000003] user= 0.00% sys= 0.00% alloc= 0b/s - Finalizer
[000004] user= 0.00% sys= 0.00% alloc= 0b/s - Signal Dispatcher
[000005] user= 0.00% sys= 0.00% alloc= 0b/s - Attach Listener
[000009] user= 0.00% sys= 0.00% alloc= 0b/s - Framework Active Thread
[000012] user= 0.00% sys= 0.00% alloc= 0b/s - Framework Event Dispatcher
[000014] user= 0.00% sys= 0.00% alloc= 0b/s - Start Level Event Dispatcher
[000015] user= 0.00% sys= 0.00% alloc= 0b/s - Bundle File Closer
[000018] user= 0.00% sys= 0.00% alloc= 0b/s - [Timer] - Main Queue Handler
[000019] user= 0.00% sys= 0.00% alloc= 0b/s - Worker-JM
[000029] user= 0.00% sys= 0.00% alloc= 0b/s - [ThreadPool Manager] - Idle Thread
[000030] user= 0.00% sys= 0.00% alloc= 0b/s - Java indexing
[000033] user= 0.00% sys= 0.00% alloc= 0b/s - com.google.inject.internal.util.$Finalizer
Related Topics
Looking for an Example for Inserting Content into the Response Using a Servlet Filter
Remove() on List Created by Arrays.Aslist() Throws Unsupportedoperationexception
Why Does Concurrenthashmap Prevent Null Keys and Values
What Is the Default Access Modifier in Java
Square Root of Bigdecimal in Java
Can an Array Be Used as a Hashmap Key
How to Customise the Jackson JSON Mapper Implicitly Used by Spring Boot
"Detached Entity Passed to Persist Error" with JPA/Ejb Code
Clean Way to Combine Multiple Jars? Preferably Using Ant
How to Refer Environment Variable in Pom.Xml
How to Check CPU and Memory Usage in Java
Multipart File Upload Using Spring Rest Template + Spring Web MVC
Assert Equals Between 2 Lists in Junit
Increase the Java Heap Size Permanently
Why Is Class.Newinstance() "Evil"
Can "This" Ever Be Null in Java
Take N Random Elements from a List<E>
Java Conditional Compilation: How to Prevent Code Chunks from Being Compiled