We often need to find CPU and memory utilization of a particular Java process in order to get some performance stats, capacity planning or just to understand the current load on system. Performance monitoring and testing is integral part of any Java application development, especially if you are working in high frequency trading space. In order to fix a performance problem, you have to find them first and knowledge of right tools and commands helps there. One of the primary cause of poor application performance be it Java or any other process is exhaustion of important resources like CPU and memory. Monitoring CPU and memory utilization of your Java process gives you some useful insight like what is normal memory and CPU utilization, when are you getting spikes on CPU and memory consumption. If heap memory is constantly increasing over time than it’s an indication that your Java application may have a memory leak. In this Java performance tutorial, we will learn about two most common Solaris commands prstat and pmap to find CPU and memory utilization of Java process. prstat is a process monitoring tool easily available in any Solaris systems including SPARC and x86 boxes, which provides useful statics of CPU and memory utilization of a process. For other systems e.g. windows you can use windows task manager for CPU monitoring and Linux you can use top command for same purpose. By the way, you can also check out Java performance by Charlie Hunt and BinuJohn, it is immensely helpful for Java performance monitoring and tuning. This is the book, which gives you right kind of tools and knowledge required to find performance stats for Java application.
Solaris command to find CPU and Memory utilization of Java process
prstat command in UNIX, particularly in Solaris can be used to find memory and CPU consumption of a Java application. prstat needs, process id to show statics of particular process, which can be obtained by using ps command in UNIX, as shown below.
1. Find the PID of Java process
2) Find CPU and Memory usage of Java process
Now most important thing is to understand output of prstat command, two columns which are most important are RSS and and CPU. If you look man page for Solaris prstat command, RSS denotes total amount of physical memory used by process and CPU denotes total percentage of CPU used by that Java process. RSS stands for Resident Set Size and shows total physical memory used by process on Kilobytes(K), Megabytes(M) and Gigs(G). On the other hand we have another memory stats denoted by SIZE which shows total size of virtual memory used by Java process or any other process including mapped files and devices. SIZE denotes size of virtual memory in Kilobytes(K), Megabytes(M) or Gigabytes(G).
TIME denotes total time duration for which that particular Java process is running.
USERNAME shows the Solaris user account on which process is started and running.
PRI denotes priority of your Java process. Higher the number means Higher the priority of Java process you are monitoring.
You can see that prstat is very useful command and also easily available on almost every Solaris machine, It answer fundamental questions like How much CPU and memory is currently utilized and which process are using those. It also provides useful details related to process/threads. You can also run prstat command with an interval as in above example CPU and memory stats will be updated every five seconds. You can also record this data to analyze CPU and memory utilization over a period of time to find issues and performance stats. Simply redirect output of prstat command to a file. If you are interested in further reading than second chapter from Java performance book, is another great read on this topic, which discusses about CPU utilization, memory utilization, network I/O utilization and disk I/O utilization on all major operation system including Windows, Linux, and Solaris.
Some of the useful option of PRSTAT command in Solaris:
Following are some of the useful options of prstat utility, worth remembering.
interval You can define interval in seconds after which prstat will update CPU and memory utilization
-s To sort the output of prstat, you can sort output of prstat command in Solaris on CPU, pri, rss, size, time columns.
-c To run prstat in continuous mode, which print new stats after specified interval in new lines instead of overwriting existing details, this can be used to record prstat output over period of time. by running prstat into continuous mode we can generate useful reports which shows CPU and memory utilized by Java process by an hour, a day or a week depending upon duration on which you run prstat in continuous mode.
Another command which can be used to find memory utilization of a process in Sun Solaris operating system is "pmap". but there is one limitation of pmap command, you can only run pmap command with the user id which is used to start the corresponding Java process, other wise you will get permission denied error as shown below :
pmap command in Solaris shows address information of a Java process
That’s all on How to find CPU and Memory usage of a Java program in Solaris. If you are working in Linux environment and don’t have access to prstat process monitoring utility, you may want to check top and pstree, which are Linux equivalent of Solaris prstat. On closing noted, Java performance book by Charlie hunt and Binu John mentioned in my list of must read books for Java developer, has several techniques and command to find memory and CPU utilization in Windows, Solaris and Linux. If you are serious about mastering performance tuning and benchmarking Java application, they you should spend some time with this book. One of the best book for performance monitoring and tuning Java application. At the very least you can plug JConsole to your Java application and monitor memory usage, thread usage and deadlock
Related Java and UNIX command tutorials from Javarevisited