Swap
lastmod 2025-02-14created 2015-05-2828 edits
Categories
Examples
- DiskPartitioning:Example:UEFI_with_GPT: How to partition UEFI with GPT using fdisk.
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:
Detach the existing swap file (/swapfile) from the system:
Remove the swap file:
If you are only removing it, remove its entry from /etc/fstab.
Create an empty file with the fallocate command:
Change permissions so that only the system can access the swap file:
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:
Check that it is attached:
If needed, register it in /etc/fstab:
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.
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.
The frequency can be set to a value between 0 and 100.
Setting it to 0 as follows means swap is not used:
To apply it on system reboot, add the following to /etc/sysctl.conf:
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
- Memory structure and the Swap area
- What is a Linux swap (SWAP) partition? What does it do?
- KLDP: Do we still need a swap partition these days?
- What is a Linux swap (SWAP) partition? What does it do? :: Sergeswin @IT. Blog Magazine
- [Recommended] Swap - ArchWiki
- Creating a swap (Swap) file
- Recommended Linux swap space size - ZETAWIKI
- [Recommended] Linux Partition HOWTO - 4. Partitioning requirements
- What is the recommended swap size for Red Hat Enterprise Linux? - Red Hat Customer Portal