Linux Swap
Table of Contents
- General idea
- Changing swappiness value temporarily
- Set swappiness value permanently
- Using swap file (instead of swap partition)
General idea
Swapping is a technique where data in Random Access Memory (RAM) is written to a special location on your hard disk—either a swap partition or a swap file—to free up RAM. Based on Linux documentation:
"This control is used to define how aggressive (sic) the kernel will swap memory pages.
Higher values will increase aggressiveness, lower values decrease the amount of swap.
A value of 0 instructs the kernel not to initiate swap until the amount of free and
file-backed pages is less than the high water mark in a zone.
The default value is 60."
Changing swappiness value temporarily
sudo sysctl vm.swappiness={value}
Set swappiness value permanently
Open /etc/sysctl.conf file and add this line.
vm.swappiness={value}
Using swap file (instead of swap partition)
- Create a swapfile
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576/swapfileis the path and name of the swap file. You can change this to something else.
The number forcount=1048576equals 1GB. To increase size, multiply that number by 4 if 4GB swap is desired. - Set the swap file permission to 600
sudo chmod 600 /swapfile - Format the newly created file as swap
sudo mkswap /swapfile - Enable the newly created swap file
sudo swapon /swapfileTo verify if the new swap file is in use, run
swapon -s - Add entry to
/etc/fstab/swapfile none swap sw 0 0