Bash – Check Memory Consuming Processes

| | | | | | | | | | | | | | |

Basic Version – This should work on every UNIX system.

Bash
top -o mem

Get top ten memory consuming processes in one line

Bash
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -11

Get top ten memory consuming processes in an easier to read script.

Bash
#!/bin/bash
# mem_check.sh - Find top memory-consuming processes

echo "--- Top 10 Memory Consuming Processes ---"
printf "%-10s %-10s %-10s %s\n" "PID" "%MEM" "RSS(MB)" "COMMAND"

# ps -eo: custom output format
# rss: Resident Set Size (actual physical memory used in KB)
# %mem: Percentage of total RAM
ps -eo pid,%mem,rss,args --sort=-%mem | head -n 11 | tail -n 10 | while read -r pid pmem rss comm; do
	# Convert RSS from KB to MB for easier reading
	rss_mb=$(echo "scale=2; $rss / 1024" | bc)
	printf "%-10s %-10s %-10s %s\n" "$pid" "$pmem" "$rss_mb" "$comm"
done

Other UNIX commands to help with memory consumption

  • free -h – This command will provide a rather quick overview of total, used, and available memory in human-readable format (MB/GB).
    • NOTE: This command doesn’t natively work on macOS
  • top – This command displays a real-time list of processes.
    • If you press Shift+M while it is running the command will sort the processes by memory usage.
    • Optionaltop -o mem this will automatically sort by memory usage.
  • htop – This is an interactive user-friendly version of top that makes it easier to scroll through the details.
    • NOTE: This command doesn’t natively work on macOS but can be installed with brew install htop
  • smem – Not available on every system but this will provide a more accurate measure of the process’s memory.
    • NOTE: This command doesn’t natively work on macOS
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.