When an Ubuntu server or desktop experiences unexpected slowdowns, high load averages, or out-of-memory (OOM) killer terminations, identifying the resource-heavy process immediately is crucial. While interactive utilities such as top and htop provide dynamic visual dashboards, they cannot be piped easily into log files, automated shell alerts, or remote SSH diagnostic commands.
The standard Linux process status utility, ps, features robust sorting arguments that allow you to generate formatted, instant snapshots of the highest memory-consuming and CPU-consuming tasks running on your system.
Sorting Processes by Memory Consumption (RAM)
To inspect the top 10 processes consuming physical RAM in Ubuntu:
ps aux --sort=-%mem | head -n 11
Here is what each flag achieves:
a: Shows processes belonging to all users across the system.u: Displays user-oriented columns including memory percentage, CPU usage, and start time.x: Includes background processes not attached to an active terminal.--sort=-%mem: Sorts output in descending order by percentage of physical memory consumed (the leading minus sign signifies descending order).head -n 11: Displays the header row plus the top 10 resource-hungry processes.
Sorting Processes by Processor Usage (CPU)
To identify applications causing CPU utilisation spikes:
ps aux --sort=-%cpu | head -n 11
This command immediately highlights compilation tasks, web server worker threads, database queries, or looping background scripts monopolising processor cores.
Displaying Custom Columns for Cleaner Output
The default aux output produces very wide text lines. To display a streamlined table containing only the Process ID (PID), User, CPU percentage, Memory percentage, and executable command:
ps -eo pid,user,%cpu,%mem,comm --sort=-%mem | head -n 11
To show exact resident memory usage in kilobytes (RSS) rather than a percentage:
ps -eo pid,user,rss,%mem,comm --sort=-rss | head -n 11
Exporting Memory Diagnostics to a Log File
For automated server health monitoring or attaching diagnostics to a support ticket, redirect the output directly to a timestamped file:
ps aux --sort=-%mem | head -n 25 > /tmp/high_memory_processes.txt
This provides a clean, portable record of system state without interrupting background server daemons.