-
Notifications
You must be signed in to change notification settings - Fork 1
I. SWAP Memory
Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition or a swap file. In most cases, when running Linux on a virtual machine, a swap partition is not present, so the only option is to create a swap file. Better version: As software allocates more virtual memory the kernel commits to providing more physical memory; but often the physical memory isn't actually used/allocated (e.g. due to data from memory mapped files that remains unaccessed, copy on write data that isn't modified, etc). This can lead to a situation where the kernel refuses to allocate more virtual memory when there's plenty of free physical memory. There are 2 solutions to this:
a) "overcommit". This is where the kernel lies (guarantees it can provide more virtual memory than it can) and becomes a dodgy piece of shit ("OOM killer" randomly trashing everything that matters because the kernel actually needed to provide memory that it lied about being able to provide).
b) swap space to extend physical memory. Ideally this is balanced such that all of physical memory is used and almost no swap space is used (swap space is merely set aside to cover memory the kernel promised it can provide but isn't actually being used); but it can also be used to allow more "actually used" virtual memory with some performance cost (where that performance cost depends on how much swap space is actually used in a non-linear way, because "very rarely used data" on swap space costs more time rarely and "more frequently used data on swap space" costs more time more often).
Note that there is also some interaction between "physical memory plus swap" used by processes and "physical memory (but not swap)" used for (virtual) file system caches. This can lead to an unintuitive case where increasing the use of swap space reduces the amount of disk IO and improves performance; because "less frequently used data" (used by processes) is sent to swap space to allow more physical memory to be used for caching "more frequently used" file data.
-
Create a file that will be used for swap:
$ sudo fallocate -l 2G /swapfile
-
Only root user can be able to write and read the swap file. So we must set the permissions as such:
$ sudo chmod 600 /swapfile
-
Use the mkswap utility to set up the file as Linux swap area:
$ sudo mkswap /swapfile
To make the change permanent open the /etc/fstab file and append the following line:
$ vi /etc/fstab
/swapfile swap swap defaults 0 0
-
Reboot for changes to take into effect
$ sudo reboot
-
Check that the swap is active after logging in
$ sudo free -h
Results:
**Congrats, you're done!**
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100.
A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60.
-
You can check the current swappiness value by typing the following command:
$ cat /proc/sys/vm/swappiness
-
While for Linux systems that is acceptable, if we want to turn our raspberry pi into a Web Server we need to set a lower value.
$ sysctl vm.swappiness=10
-
To make this parameter persistent across reboots append the following line to the end of the /etc/sysctl.conf file : vm.swappiness=10
-
Reboot for changes to take into effect
$ sudo reboot
-
First we have to deactivate the swap by typing:
$ sudo swapoff -v /swapfile
-
Remove the swap entry
/swapfile swap swap defaults 0 0
from the/etc/fstab
file. -
Last but not least, delete the actual swap file with the rm command:
$ sudo rm -rf /swapfile
We have learned how to create a swap file and activate and configure swap space on your Linux system.
If you hit a problem or have feedback (which is highly welcomed) please feel free to get in touch, more details in the footer.