
How to Monitor and Visualize Process CPU & Memory Usage
Pinpointing performance bottlenecks in an application can be a frustrating task. Is your script consuming too much memory? Is a specific process causing unexpected CPU spikes? Answering these questions often requires more than just a momentary glance at a system monitor; it requires tracking a process’s behavior over time.
Fortunately, a simple yet powerful command-line tool exists to make this process incredibly easy. This utility allows you to record the CPU and memory consumption of any running process and generate an easy-to-understand plot, giving you a clear visual history of its resource usage.
This approach is invaluable for developers, system administrators, and data scientists who need to diagnose performance issues, optimize code, or simply understand how an application behaves under a load.
Getting Started: Installation and Basic Usage
The tool, psrecord, is a Python package that can be installed quickly using pip. To get started, open your terminal and run the following command:
pip install psrecord
Once installed, you can use it in two primary ways: by attaching it to an already running process or by using it to launch and monitor a new command from start to finish.
1. Monitoring an Existing Process
If the process you want to analyze is already running, you first need to find its Process ID (PID). You can typically find this using commands like htop
or ps aux | grep <process_name>
on Linux/macOS, or by looking in the Task Manager’s “Details” tab on Windows.
Once you have the PID, you can start recording its activity with this command:
psrecord <PID> --plot process_activity.png
Replace <PID>
with the actual Process ID. The tool will immediately begin monitoring the process. When you’re done, simply press CTRL+C to stop recording, and an image file named process_activity.png
will be saved in your current directory. This image will contain a graph showing the CPU and memory usage over the recording period.
2. Recording a New Command from Scratch
Perhaps the most useful feature is the ability to launch a command and record its entire lifecycle. This is perfect for profiling a script or application from the moment it starts until it completes.
To do this, you pass the command you want to run as the final argument, making sure to enclose it in quotes if it contains spaces or arguments.
psrecord --plot my_script_performance.png "python heavy_script.py --input data.csv"
In this example, psrecord
will execute the Python script heavy_script.py
. It will automatically monitor its resource usage and, once the script finishes, save the performance graph to my_script_performance.png
.
Advanced Monitoring Techniques for Deeper Insights
While the basic functionality is powerful, a few extra options provide even more control and detail.
Tracking Child Processes: Many applications spawn child processes to handle tasks. By default,
psrecord
only monitors the parent process. To get a complete picture of the application’s total resource footprint, use the--include-children
flag. This will aggregate the CPU and memory usage of the parent process and all of its descendants.psrecord --include-children --plot full_app_usage.png "my-complex-app"
Setting a Recording Duration: If you only want to monitor a process for a specific amount of time (e.g., during a performance test), you can set a duration in seconds. The recording will automatically stop after the specified time.
psrecord <PID> --plot 60_second_test.png --duration 60
Logging Raw Data: While plots are great for visualization, you may sometimes need the raw data for further analysis. The
--log
flag saves the CPU and memory readings to a text file, which you can then import into a spreadsheet or another analysis tool.psrecord <PID> --log activity_log.txt
Why Visualizing Performance Data Matters
Tools like top
, htop
, or the Windows Task Manager are excellent for seeing a real-time snapshot of your system. However, they don’t easily show you trends or historical behavior. A process might have brief, intense spikes in CPU usage that are easy to miss with a spot check.
By generating a historical plot of CPU and memory usage, you can instantly see:
- Memory leaks, where memory usage consistently climbs and never drops.
- CPU-intensive startup phases.
- The impact of specific functions or data processing jobs on system resources.
- The overall stability and efficiency of your application over time.
This simple, lightweight utility provides a crucial diagnostic view that is often missing from standard system monitoring toolkits. It empowers you to make data-driven decisions to improve application performance and stability.
Source: https://www.linuxlinks.com/psrecord-record-cpu-memory-activity-process/