The AWS free tier is the perfect place for throwaway projects that you need to host temporarily. Often, you’ll run into limitations, but with some creative thinking you can get around them without charging a dime to your credit card.
I once faced a problem using a t2.micro instance on AWS (but keep in mind this applies to any VPS that doesn’t have much memory) where a Python application needed more than 1 GB of memory to start. What did I do? Pay a few dollars for a bigger instance? No way!
A quick analysis revealed that I just needed a little more memory for this Python application to start. In Linux, it’s possible to add more swap space by creating a file. Swap space is an area on the hard drive that is used as memory in case the physical memory runs out. It’s slower, but it works.
The t2.micro instance that I was using had the default 8 GB of storage attached to it. Here is what the memory looked liked before adding the swap file. I’m recreating the example on a new instance, so that’s why there’s so much free memory below.
$ free -m total used free shared buff/cache available Mem: 978 122 103 0 752 698 Swap: 0 0
Note that I’m creating a swap file, not a swap partition which would take a
little more work. Creating a file and using that as swap space is the fastest
way to get more RAM. It doesn’t matter where you create the file, but I’m doing
it at the root
/ instead of in the user’s home directory because it makes
more sense. The swap space is part of the system, not the user.
$ sudo dd if=/dev/zero of=/swapfile bs=128M count=16 16+0 records in 16+0 records out 2147483648 bytes (2.1 GB, 2.0 GiB) copied, 30.8284 s, 69.7 MB/s
dd is the command, the input file
if is a bunch of null characters (0x00),
and the output file
of is the name of the swap file. The block size
128M, and we are creating 16 of these blocks. The block size should be
relatively small since it’s the minimum amount of space that a file will take
up. A very large block size would be a waste of space. It’s like one person
living in a very big house.
Next, we need to update the permissions and activate the swap space.
$ sudo chmod 600 /swapfile $ sudo mkswap /swapfile Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=e50110e7-9509-497d-945e-a03329a631d4 $ sudo swapon /swapfile $ sudo swapon -s Filename Type Size Used Priority /swapfile file 2097148 0 -2
The last command allows us to verify that the steps were successful. We can also run the first command to see our swap space. The output of this command is easier to understand.
$ free -m total used free shared buff/cache available Mem: 978 122 201 0 654 705 Swap: 2047 0 2047
If you reboot the machine, the swap space will be turned off. We can edit /etc/fstab to have the swap space turned on at boot.
$ sudo vim /etc/fstab
Add the following line to the file.
/swapfile swap swap defaults 0 0
Although not ideal, this is a quick way to get more memory, and you don’t even have to reboot the machine. The downsides are that you lose disk space, and if you are on a cheap VPS you probably didn’t have much in the first place, it’s not as fast as physical RAM, and some cloud providers might charge for read/write to external storage. In the worst case, thrashing might cause you to run up quite the bill.
In my situation, where I was happy to get any memory just so the program could start and I wouldn’t have to leave the free tier, the swap file was a pretty good solution.Found a mistake? Edit this post.