When you first set up a Linux box on a service like DigitalOcean for example, your physical RAM is the only memory your system can use. That means if you go with the basic service of 512MB, no process can use more than that. And given that there'll be a lot of processes running on your server, that limited memory is going to be used up fast.
To combat this problem, we have the idea of swap space, swap files, or virtual memory. Hard disk space is a lot cheaper than RAM, and there is a lot more of it. At the expense of being slower. While RAM is scarce, hard disk space almost never is. And if it is scarce, it's easy to buy more. Swap files take advantage of this, by trying to extend the RAM and use physical disk space as well as just "pure" RAM.
So while we might have 512MB RAM, we can double, or even triple that designating parts of our hard disk as "virtual memory". This might seem like cheating, but it's really useful! The system creates these "swap files" which juggle memory into and out of actual RAM to optimize system performance. As a result, processes can use a lot more memory than the mere basic RAM that comes equipped with a system.
Here's how to create a swap file on your Linux box:
Step 1: Check your Existing Swap File Information
Before creating any swap files or extending your virtual memory, first use the "free" command to check and see if you already have any existing swap memory. Just type:
Here's the output on a fresh Linux box:
As you can see, the "Swap" section is "0". Whereas we have 498M of total RAM. The "-m" parameter shows us the values in MB. So since we don't have any swap memory, our existing processes ca only use 498 MB of RAM. That's very low! Let's extend it by using some of our physical hard disk space to supplement it.
Step 2: Create a File of the Right Size and Assign Permissions
One way of creating a swap space, is to have a separate partition. But we can also just designate a file to be the swap file. It's easier, and more convenient. No need to partition a hard disk, format it etc. Since we have 498 MB of RAM, I'm going to allocate an extra 1GB of memory on the physical disk. So I create a file with the specified size like this:
fallocate -l 1G /swapmemory
As you can see below, this creates a file with a size of 1GB called "swapmemory" in the home directory:
Now we want to ensure that only the owner can read and write to it. So we change the permissions accordingly:
chmod 600 /swapmemory
Step 3: Prepare the File and Designate it as "Swap"
What we've created is a blank file. Before we can assign it as virtual memory, we need to first configure it as such. We do that with the "mkswap" command:
Once the swap space has been set up, we can finally enable it as a swap file:
Now when we run the "free -m" command again, we can see that the swap memory has been set up for us:
We now have a swap space of 1023 MB (~ 1GB).
Step 4: Make this Swap File Permanent
Right now, the swap file won't be used when you reboot the system. To ensure that this happens, we need to modify the /etc/fstab file and add a new entry into it like this:
echo '/swapmemory none swap sw 0 0' | tee -a /etc/fstab
Save the file and you're done. Now the next time your system boots, it will use the "swapmemory" file as specified in /etc/fstab.
As you can see, creating extra virtual memory on your Linux system is easy and requires very few steps. You can be up and running with a new swap file in under 15 seconds.