Disk and memory usage: df, du, free

Linux · Essentials · 8 min

Full disks and memory pressure are among the most frequent causes of outages.

df -h shows each mounted filesystem's size, used and available space in human-readable units. df -h /var shows just the filesystem that holds /var. A filesystem can also run out of inodes (one per file) while bytes are still free, typically with millions of tiny files; check with df -i.

du measures what directories contain: du -sh dir gives one total, and du -h --max-depth=1 /var | sort -h lists subdirectories by size so you can drill down. ncdu is an interactive alternative. If df reports a full disk but du cannot find the space, a process may still hold a deleted file open (often a rotated log); lsof +L1 lists such files, and restarting the process frees the space.

free -h shows memory. Linux deliberately uses spare RAM as page cache for files, so "free" is often small on a healthy machine. The number that matters is available: memory that can be handed to programs, including reclaimable cache. Swap is disk space used as overflow; some swap use is fine, but constant swapping in and out makes everything slow.

On servers, keep an eye on /var (logs, databases, Docker images in /var/lib/docker) and set up log rotation and monitoring before the disk fills.

Example

df -h                          # free space per mounted filesystem
df -h /var                     # only the filesystem holding /var
df -i /                        # inode usage: "No space left" with free bytes?
du -sh ~/Downloads             # total size of one directory
sudo du -h --max-depth=1 /var 2>/dev/null | sort -h | tail -n 5   # biggest subdirs
sudo lsof +L1                  # deleted files still held open
free -h
#        total   used   free  shared  buff/cache  available
# Mem:    15Gi  4.1Gi  1.0Gi   0.5Gi        10Gi       10Gi
# Swap:  2.0Gi     0B  2.0Gi

Remember

  • df shows free space per filesystem, du shows what uses it
  • df -i for inodes; lsof +L1 for deleted-but-open files
  • In free, read 'available', not 'free'

Watch out for

  • Panicking because free shows little free memory, when most of it is reclaimable page cache.

Check yourself

  1. df says the disk is 100% full but du on / totals much less. A likely cause?
  2. Which column of `free -h` best tells you how much memory new programs can use?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: man7.org/linux/man-pages/man1/df.1.html