Skip to content
Search

Swap

lastmod 2025-02-14created 2015-05-2828 edits

Categories

Examples

Linux swap sapce

Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Recommendation 1. Twice the memory

  • Generally set to about twice the memory size.
  • But at least 256MB.

Recommendation 2. Red Hat recommendation (RHEL 6)

Amount of RAM

Recommended swap space

Recommended swap space if using hibernation

Less than 2 GB

2 times the amount of RAM

3 times the amount of RAM

2 ~ 8 GB

Equal to the amount of RAM

2 times the amount of RAM

8 ~ 64 GB

At least 4 GB

1.5 times the amount of RAM

More than 64 GB

At least 4 GB

(Hibernation not recommended)

  • Hibernation (or suspend to disk) is a feature that writes all contents of system memory to non-volatile storage such as a hard disk before powering off the system.

Swap file creation script (simple ver)

If you don't want to read the details below, just see the following.

SWAPFILE_PATH=/swapfile2
fallocate -l 8GB "${SWAPFILE_PATH}"
chmod 600 "${SWAPFILE_PATH}"
mkswap "${SWAPFILE_PATH}"
swapon "${SWAPFILE_PATH}"
echo "${SWAPFILE_PATH}    none    swap    sw    0    0" >> /etc/fstab
swapon -s

How to check/create/register/detach/remove a swap file

Check swap memory:

$ free -m
              total        used        free      shared  buff/cache   available
Mem:          15418        6786        3986         161        4645        8149
Swap:          2047        1376         671

Check whether a swap file is set:

$ swapon -s
Filename                                Type            Size    Used    Priority
/swapfile                               file            2097148 1409048 -2

Detach the existing swap file (/swapfile) from the system:

swapoff /swapfile

Remove the swap file:

rm /swapfile

If you are only removing it, remove its entry from /etc/fstab.

Create an empty file with the fallocate command:

root@your-13UD70P:/# fallocate -l 8GB /swapfile

Change permissions so that only the system can access the swap file:

root@your-13UD70P:/# chmod 600 /swapfile

Format the empty file as a swap file:

root@your-13UD70P:/# mkswap /swapfile
Setting up swapspace version 1, size = 7.5 GiB (7999995904 bytes)
no label, UUID=0ae793bf-7bb3-4a19-8de8-b7ce5b744c00

Attach the swap file to the system:

swapon /swapfile

Check that it is attached:

root@your-13UD70P:/# swapon -s
Filename                                Type            Size    Used    Priority
/swapfile                               file            7812496 0       -2

If needed, register it in /etc/fstab:

/swapfile    none    swap    sw    0    0

How to reset swap memory

Swap memory can be used even when there is plenty of memory. Since the used swap memory affects system performance, it is better to release it.

swapoff -a
swapon -a

Adjusting swap usage frequency

If swap memory is used often even though there is plenty of physical memory, you can adjust how often swap is used.

cat /proc/sys/vm/swappiness
60

The frequency can be set to a value between 0 and 100.

Setting it to 0 as follows means swap is not used:

echo 0 > /proc/sys/vm/swappiness

To apply it on system reboot, add the following to /etc/sysctl.conf:

vm.swappiness = 0

How to check swap memory usage per process

  • In htop, add M_SWAP under F2 -> Columns. (Some versions don't have this column)
  • In top, press f and add the SWAP field.
  • Use smem. Simply smem -t
  • Check directly with grep Swap /proc/*/status.
  • Use vmstat (system-wide swap usage) vmstat -s | grep "used swap"
  • Use free. (total swap usage) free -m

See also

Favorite site

author Zer0 sitemap