Friday, July 30, 2021

How to find CPU and Memory used by Java process in Solaris – prstat command example

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 the system. Performance monitoring and testing is an integral part of any Java application development, especially if you are working in a high-frequency trading space. In order to fix a performance problem, you have to find them first and knowledge of the right tools and commands helps there. One of the primary causes of poor application performance is it Java or any other process is the 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 the normal memory and CPU utilization, when are you getting spikes in CPU and memory consumption. If heap memory is constantly increasing over time then it’s an indication that your Java application may have a memory leak. 

In this Java performance tutorial, we will learn about the two most common Solaris commands prstat and pmap to find CPU and memory utilization of the Java process. prstat is a process monitoring tool easily available in any Solaris system including SPARC and x86 boxes, which provides useful statics of CPU and memory utilization of a process. 

For other systems like windows, you can use windows task manager for CPU monitoring, and for Linux you can use the top command for the same purpose. By the way, you can also check out these Java performance courses to learn more bout tools and techniques for performance improvement in Java. 

it is immensely helpful for Java performance monitoring and tuning. This is the course, which gives you the right kind of tools and knowledge required to find performance stats for Java applications, including troubleshooting memory leaks in Java.





Solaris command to find CPU and Memory utilization of Java process

Solaris prstat command example cpu and memory in Java programprstat 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

ps -ef | grep "MyJavaSever"

2) Find CPU and Memory usage of Java process

prstat -p 28983 5
PID    USERNAME  SIZE   RSS    STATE   PRI     NICE      TIME          CPU     PROCESS/NLWP
29389  appsvs    1129M  445M   sleep   47      4         0:03:44       0.0%    java/49

Now most important thing is to understand output of prstat command, two columns which are most important are RSS and CPU. If you look man page for Solaris prstat command, RSS denotes total amount of physical memory used by the process and CPU denotes the total percentage of CPU used by that Java process. 

RSS stands for Resident Set Size and shows total physical memory used by the process on Kilobytes(K), Megabytes(M) and Gigs(G). On the other hand, we have another memory stats denoted by SIZE which shows the total size of virtual memory used by Java process or any other process including mapped files and devices. SIZE denotes the 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 29389
pmap: cannot examine 29389: permission denied

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 a Linux environment and don’t have access to prstat process monitoring utility, you may want to check top and pstree, which are the Linux equivalent of Solaris prstat

In closing noted, the Java performance book by Charlie Hunt and Binu John mentioned in my list of must read books for Java developers, 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 applications, then you should spend some time with this book. One of the best books for performance monitoring and tuning Java applications.  At the very least you can plug JConsole into your Java application and monitor memory usage, thread usage, and deadlock


Related Java and UNIX command tutorials from Javarevisited

2 comments:

  1. Jconsole is best to monitor Memory usage in Java application. You can view real time memory statistics in JConsole, doesn't matter which operating system, platform your application is running. Just make sure, it's listening on JConsole port.

    ReplyDelete
  2. JConsole is a great visual tool and it provides detailed info about each type of memory, thread and classes. I cant see any advantage of these commands over jconsole.

    ReplyDelete