This tutorial will explain how to setup swap ram on your machine (Namely Ubuntu 18.04)
Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM. Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.
The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.
- To see if we have any swap ram setup, we use the following command:
free -husually give some output like:
total used free shared buff/cache available Mem: .... .... .... ...... .......... ......... Swap: 0B 0B 0B
Or if there is actually any swap ram setup:
total used free shared buff/cache available Mem: .... .... .... ...... .......... ......... Swap: 8.0G 0B 8.0G
Incase you do happen to have any swap memory you can always use:
swapon --show to show you any active swapfiles.
Now to create a new swapfile theres a sequence of commands we must execute to dedicate space on your harddrive:
- Lets first see how much available storage we have to know how much space we can assign to the swapfile using:
df -hOften with output resulting in:
Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 798M 85M 713M 11% /run /dev/vda1 87G 9.9G 73G 13% / <-- Normally we use the root mounted filesystem tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 798M 0 798M 0% /run/user/0
- As you can see ther, in the example, we have 73 GiB available to use however we like, including the for swapfile, to create one, we must make a new file with its size allocated to the amount of M (MB), G (GB), K (KB), we would use:
sudo fallocate -l 1G /location_of_swapfile
- Next we edit permissions to make sure only the root user can read/write from the swapfile as such:
sudo chmod 600 /location_of_swapfile
- Now we run
mkswapto prep the file for usage as swap ram:
sudo mkswap /location_of_swapfileoften outputting:
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=9d357042-245b-4123-8fa2-1e7e8b1eb1ab
- Now to enable the swapfile we run the command:
sudo swapon /location_of_swapfile
swapon --showshould now show the new swapfile:
NAME TYPE SIZE USED PRIO /location_of_swapfile file 1024M 0B -2
- To make this change persist through restarts of your machine, we must edit the /etc/fstab file on your machine, running:
echo '/location_of_swapfile none swap sw 0 0' | sudo tee -a /etc/fstabThis effectively adds it to startup and initialize whenever your machine boots up.
To disable swapfiles simple type in:
swapoff /location_of_swapfile, you can freely delete such file now, but be warned you must also remove the newly added line from
/etc/fstab so that your machine does not try to initialize the swapfile upon reboot.