Adding Swap on AWS EC2 Instances

/
175

Sometimes, especially with frameworks that require build steps on cloud instances, you will need a bit more memory to work with.

Many cloud instance images do not have swap enabled by default, for a number of reasons, but it is not difficult to enable swap with a few commands.

Checking your current memory stats

First, let's check to see if we have any swap enabled already, using the free command, I've opted to use -h to make it "human-readable"

$ free -h
               total        used        free      shared  buff/cache   available
Mem:           419Mi       291Mi        15Mi       0.0Ki       111Mi        97Mi
Swap:             0B          0B          0B

There's our problem! :) We definitely need some swap space.

Creating the swapfile

Let's first create our swapfile which is the storage that the system will use on the disk, it is a way to pre-allocate the space so we dont run into issues trying to allocate it later on if we need it and the disk is full.

Create a Swap File

First, decide on the size of the swap file. A good rule of thumb is to make it equal to or double the size of your RAM, depending on your system usage. For this example, let's create a 1GB swap file.

sudo fallocate -l 1G /swapfile

If fallocate is not available, you can use dd:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Set Proper Swap File Permissions

For security, the swap file must not be readable by any user other than root. Set the correct permissions:

sudo chmod 600 /swapfile

Format the File for Swap

Next, format the file to be used as swap space:

sudo mkswap /swapfile

Enabling the shiny new swapfile

Next, we need to tell the system to start using the swap file, otherwise it won't know it exists.

This is pretty easy, it's just one command :)

sudo swapon /swapfile

Check if the swap is active by again using the free -h command:

free -h

You should now see the swap has been enabled:

            total        used        free      shared  buff/cache   available
Mem:           419Mi       291Mi        15Mi       0.0Ki       111Mi        97Mi
Swap:          1.0Gi          0B       1.0Gi

Making the swapfile permanent

Now we need to make sure that the swap file is enabled on boot, so that we don't have to do this every time we restart the instance.

To ensure the swap file is reactivated on boot, add it to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Restart the instance to test that the swap file automatically mounts on boot.

sudo reboot

Adjusting the swappiness and cache_pressure values

Tweak Swappiness (Optional)

The vm.swappiness parameter defines how often your system uses swap. It can range from 0 to 100. A lower value means less reliance on swap, while a higher value increases its usage. Adjust this according to your needs:

sudo sysctl vm.swappiness=10

To make this change permanent, add vm.swappiness=10 to /etc/sysctl.conf.

Optimize Cache Pressure (Optional)

Adjusting vm.vfs_cache_pressure is another way to optimize system performance. This setting affects the kernel's tendency to reclaim the memory which is used for caching of directory and inode objects.

sudo sysctl vm.vfs_cache_pressure=50

Add vm.vfs_cache_pressure=50 to /etc/sysctl.conf for a permanent change.

Conclusion

By following these steps, you can enable swap space on your EC2 instance, improving its performance under memory pressure, or preventing it from crashing or hanging due to an out-of-memory condition.


TOY MODE
π