Home/Engineer/OS / Systems/Memory and Swap Management —…
OS / SystemsIntermediateUbuntuDebianCentOSRHELOOMvmstatfree

Memory and Swap Management — free, vmstat, Swap Configuration

A practical guide to checking server memory status, analyzing OOM causes, creating swap files, and tuning swappiness.

Contents
  1. Checking Memory Status
  2. Detailed Memory Analysis
  3. Checking OOM (Out of Memory) Events
  4. Creating a Swap File
  5. Tuning swappiness

Checking Memory Status

Bash
#
free -h

# example:
#               total  used   free   shared  buff/cache  available
# Mem:           7.6G  2.1G   3.2G     45M        2.3G      5.1G
# Swap:          2.0G  100M   1.9G

# "available" (buff/cache )

Detailed Memory Analysis

Bash
# /proc/meminfo
cat /proc/meminfo | grep -E "MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree"

#
ps aux --sort=-%mem | head -10

# vmstat + + CPU (1 )
vmstat 1 10

Checking OOM (Out of Memory) Events

Bash
# OOM killer
journalctl -k | grep -i "oom\|killed process\|memory"

# OOM score check
cat /proc/$(pgrep myapp)/oom_score

# OOM (-1000: )
echo -1000 > /proc/$(pgrep sshd)/oom_score_adj

Creating a Swap File

Cloud servers (AWS t2, t3) have no swap by default.

Bash
# 4GB file
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

# boot when
echo '/swapfile none swap sw 0 0' >> /etc/fstab

# check
swapon --show
free -h

Tuning swappiness

swappiness controls how aggressively the kernel uses swap instead of RAM (0–100, default 60).

Bash
# check
cat /proc/sys/vm/swappiness

# when
sysctl vm.swappiness=10

# settings
echo 'vm.swappiness=10' >> /etc/sysctl.conf
sysctl -p
  • Database servers: vm.swappiness=10 (prefer RAM)
  • General-purpose servers: vm.swappiness=10–30
  • The default of 60 is designed for desktops
#OOM#vmstat#free#linux
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗

Questions & answers

Ask a question about this guide. We'll review and reply.