Back to lessons

Linux Survival Basics

Find the Processes Using Memory

You need a quick process-level view of memory usage.

Command

ps -eo pid,comm,%mem,%cpu --sort=-%mem | head

What changed

Nothing changes. The command lists processes sorted by memory percentage.

Danger

safe

When to use it

Use this before killing processes or resizing a VPS.

When not to use it

Do not kill a process just because it uses memory; understand its role first.

Undo or recovery

No state is changed.

Expected output

A process table sorted by memory usage.

demo script

Disposable terminal steps

  1. ps -eo pid,comm,%mem,%cpu --sort=-%mem | head
  2. free -h
  3. df -h

simulated output

What it looks like

disposable vessel
::fixture-ready::
$ ps -eo pid,comm,%mem,%cpu --sort=-%mem | head
    PID COMMAND         %MEM %CPU
     14 ps               0.0  0.0
     13 bash             0.0  0.0
      1 bash             0.0 28.5
     12 timeout          0.0  0.0
     15 head             0.0  0.0
::exit-code::0
$ free -h
               total        used        free      shared  buff/cache   available
Mem:           125Gi        46Gi       3.8Gi       259Mi        75Gi        79Gi
Swap:             0B          0B          0B
::exit-code::0
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay         3.6T  1.2T  2.3T  35% /
tmpfs            64M     0   64M   0% /dev
shm              64M     0   64M   0% /dev/shm
tmpfs            64M  8.0K   64M   1% /tmp
tmpfs            64M  352K   64M   1% /var
/dev/nvme0n1p2  3.6T  1.2T  2.3T  35% /lab/demo.sh
tmpfs            63G     0   63G   0% /proc/asound
tmpfs            63G     0   63G   0% /proc/acpi
tmpfs            63G     0   63G   0% /proc/scsi
tmpfs            63G     0   63G   0% /sys/firmware
tmpfs            63G     0   63G   0% /sys/devices/virtual/powercap
::exit-code::0

YouTube Short

Find memory-heavy processes.

Before resizing a server or killing random processes, sort the process list by memory.

LinkedIn hook

The server felt slow. Memory pressure was the first thing to rule out.

Question: What do you check first when a VPS feels slow?

experiments

A/B tests to run

Metric: linkedin_comment_rate

A: Find the processes using memory.

B: Before resizing the VPS, run this.