How to Set Swap Memory on an AWS EC2 Instance

How to Set Swap Memory on an EC2 Instance

Amazon EC2 instances typically don’t come with pre-configured swap space, as they’re usually optimized to use only RAM. However, you can manually add swap space by following some steps. Based on my experience, I use an EC2 t2.micro free tier instance with 1GB RAM. Sometimes when I use a lot of resources, the system starts to throttle. To address this without scaling up immediately, I added 2GB of swap space, and it helped the system perform better during these intensive moments. This can be a cost-efficient solution before considering an instance upgrade.

What is Swap Memory in Linux?

Swap memory in Linux is a space on a disk that is used when the physical RAM is fully utilized. When the system runs out of physical memory, it moves inactive pages of memory to the swap space to free up RAM for active processes. This process is called “swapping”. Swap helps to prevent the system from crashing or slowing down by providing extra memory space, though it is slower than RAM because accessing a disk is much slower than accessing physical memory.

Setup

  1. Check for Existing Swap:Run this command to check if any swap is already configured:
    swapon --show

    If there’s no output, then there is no swap space configured.

  2. Create a Swap File:Choose a size for your swap file. For example, if you want to create a 2GB swap file:
    sudo fallocate -l 2G /swapfile

    If fallocate is not available, use:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
  3. Set the Correct Permissions:Make the swap file secure by adjusting its permissions:
    sudo chmod 600 /swapfile
  4. Mark the File as Swap:Set up the file as swap space:
    sudo mkswap /swapfile
  5. Enable the Swap:Activate the swap file:
    sudo swapon /swapfile
  6. Make the Swap Permanent: To ensure the swap space is persistent across reboots, open the /etc/fstab file with sudo vi /etc/fstab and add the following line:
  7. /swapfile   none    swap    sw    0   0
  8. Verify:Check that the swap is active:
    swapon --show
    free -h

How Much Swap Memory is Needed?

The recommended amount of swap memory typically depends on the amount of physical memory (RAM) in the system and the workload. Some general guidelines are:

  • For systems with up to 2GB of RAM:It’s recommended to have swap space twice the size of the physical memory (i.e., 2x RAM). This ensures that there is enough swap for large memory needs.
  • For systems with 2GB to 8GB of RAM:Swap space should be equal to or up to 1.5x the size of the RAM. For example, if you have 4GB of RAM, you might set 4GB to 6GB of swap space.
  • For systems with more than 8GB of RAM:Swap space should generally be equal to the size of RAM or slightly less. High-memory systems don’t require as much swap space as they have more physical memory available.

However, these are just guidelines, and the ideal swap size can vary based on your specific workloads. For example, memory-intensive applications (like large database systems) might benefit from additional swap space, while smaller, lightweight applications might not need much at all.

Swap memory is a helpful feature in Linux for managing memory usage. While it’s slower than physical RAM, having swap can improve system stability by allowing more processes to run when memory resources are tight. Setting up swap memory can be a practical option for EC2 users before considering scaling up their instance. My free-tier instance, with 2GB of swap space, performs well for hosting WordPress and running auto-posting Python scripts.

For more information, check out this helpful discussion on swap memory size: Do I need 32GB swap if I have 16GB RAM?

Leave a Comment