Memory by Process
Luke Hebert
- 2 minutes read - 305 wordsWhen a Linux box starts feeling sluggish, the fastest way to figure out what is hogging RAM is to group processes by name and rank them by memory. The command below does exactly that in a single pipeline, no extra tools required.
The Command
ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nrWhat Each Stage Does
ps aux– Lists every running process with its resource usage.awk '{print $4"\t"$11}'– Pulls column 4 (%MEM) and column 11 (the command name) and prints them tab-separated.sort– Orders the lines so identical process names sit next to each other.uniq -c– Collapses duplicate names and prepends a count of how many instances are running.awk '{print $2" "$1" "$3}'– Rearranges the fields into a readablename count %memformat.sort -nr– Sorts the final list numerically in reverse, so the heaviest consumers appear first.
Example Output
java 3 12.4
postgres 2 8.1
node 5 3.7
sshd 1 0.2In this example, three Java instances are collectively using 12.4% of available memory, making them the top consumers on the system.
Tips
- If you want to see the actual PID alongside the name, swap
$11for$1in the firstawkcall and adjust the finalawkaccordingly. - For a live, continuously updating view,
top -o %MEMorhtopsorted by memory will give you the same information with a refresh loop. - On systems with a large number of short-lived processes, the
uniq -ccount can spike. In that case, consider adding a time filter withps aux --sort=-%mem | head -20for a simpler top-N view.
This one-liner is a quick diagnostic you can drop into a script or run interactively the next time memory pressure becomes a question.