Table of Contents
How To Create a New Swap File
Sometimes the amount of RAM that your system has is not enough for the applications you run, or maybe you're running some poorly-written software that runs fine within the RAM you have but complains about low amounts of swap space. In either case, you'll need to add more “swap” space - also known as “virtual memory” - to your system.
The guide below will demonstrate how to do this. We will be moving forward with the following assumptions (both of which will be up to you to decide):
- You want your new swap file to be
16GB
in size. - You wish to create the swap file at
/var/cache/swap/swapfile
.
Create the Swap File
The first thing you need to do is create the swapfile (obviously!). Simply create the directory (if it doesn't already exist), then use fallocate
to create your new swap file:
mkdir -p /var/cache/swap fallocate -l 16G /var/cache/swap/swapfile
Configure the Swap File
Now we need to set permissions on the swap file so that only root can read/write to it. Simply run:
chmod 600 /var/cache/swap/swapfile
And then run mkswap
to create the swap space:
mkswap /var/cache/swap/swapfile
At this point, we have a swap file located at /var/cache/swap/swapfile
that is 16GB in size and is ready to go.
Enable the Swap
Finally we need to configure our system to load the swap at boot time, and then load the swap space on our running system (or just reboot, either works). Open up /etc/fstab
in your preferred text editor, and either
Now you can either reboot to load the new swap space, OR you can run the following command to load it on the running system:
swapon /var/cache/swap/swapfile
Testing
Of course, anything you do is worth testing. To do that, simply run free -m
to show your system's memory usage. You should see something like the picture below:
Pic
Obviously your system stats will look different than mine do depending on the configuration of your system, but you should see a total virtual memory figure that resembles 16GB (16384MB) of virtual memory, or at least whatever amount of space you decided to give to it.
Also note that if you just ran the swapon
command instead of rebooting and you're migrating from one swap file to another, you may see the total size of the two swap files. To correct this, simply run swapoff </path/to/old/swapfile
and you should see a total virtual memory that corresponds to your new swap file.