Sort Linux Processes by Memory Usage with ps
List the biggest processes with ps -eo pid,%mem,rss,comm --sort=-%mem. A minus sign sorts descending, and head -n 1 keeps the header with the top entry.
The machine is short of memory and you want to know which process holds it, without opening top.
python3 -c 'import time; x = bytearray(200_000_000); time.sleep(60)' &
sleep 1
ps -eo pid,%mem,rss,comm --sort=-%mem | head -n 3
kill $! PID %MEM RSS COMMAND
18 1.5 202348 python3
20 0.0 3804 ps--sort takes a list of keys, where a leading minus means descending and a leading plus ascending, using the same names as -o. The header line is not sorted, so it stays on top and head -n 3 shows it and the first two processes, with the biggest on top. rss is the resident set in kilobytes, which here is about 200 MB for the 200 million byte array, while %mem is the same figure as a share of physical memory. See the ps manual.
RSS counts shared pages in every process that maps them, so the numbers can add up to more than the machine has; pmem carries the same flaw. Use smem or the Pss lines in /proc/PID/smaps_rollup for a fairer share. In a container, ps sees only the container's own processes.