When you need more RAM than your Fly.io machine provisioned, you can enable swap memory on the fly, allowing your memory-intensive applications to continue running without crashing due to Out-Of-Memory errors.

Let’s see this in action. Imagine you have a Python application that’s hitting its memory limits. You’d typically see MemoryError exceptions or the process being killed by the OOM killer.

Here’s a simplified Python script that might cause such issues:

import time
import sys

data = []
print("Starting memory allocation...")
try:
    while True:
        # Allocate a large chunk of memory
        data.append(b' ' * 1024 * 1024 * 50) # 50MB
        if len(data) % 10 == 0:
            print(f"Allocated {len(data) * 50} MB...")
        time.sleep(0.1)
except MemoryError:
    print("MemoryError caught! Application would likely crash here.")
    sys.exit(1)

print("This line will not be reached if memory runs out.")

If you deploy this to a small Fly.io machine (e.g., 256MB RAM) without swap, it will quickly fail.

The Problem: Running Out of RAM

Fly.io machines, by default, don’t have swap enabled. This means when your application’s memory usage exceeds the allocated RAM, the operating system’s Out-Of-Memory (OOM) killer intervenes and terminates processes to reclaim memory. For applications that have unpredictable memory spikes or genuinely require more memory than initially provisioned, this is a critical failure point.

The Solution: On-Demand Swap

You can enable swap on a Fly.io machine after it has started, effectively creating a virtual memory space on disk that the OS can use when physical RAM is exhausted. This doesn’t magically give you more RAM, but it prevents immediate OOM kills by allowing the system to page less-used data out to disk.

Here’s how you enable swap on a running Fly.io machine:

  1. SSH into your machine:

    fly ssh console -a your-app-name
    
  2. Check current swap status: Before enabling, see if any swap is already configured.

    sudo swapon --show
    

    If this command returns nothing, no swap is active.

  3. Create a swap file: We’ll create a file that will serve as our swap space. A common size is 1GB or 2GB, depending on your needs and available disk space. Let’s create a 2GB swap file.

    sudo fallocate -l 2G /swapfile
    

    fallocate is generally faster than dd for creating large files.

  4. Set appropriate permissions: The swap file should only be readable by the root user for security.

    sudo chmod 600 /swapfile
    
  5. Mark the file as swap space: This prepares the file for use by the kernel.

    sudo mkswap /swapfile
    
  6. Enable the swap file: Now, activate the swap space.

    sudo swapon /swapfile
    
  7. Verify swap is active: Run sudo swapon --show again. You should now see /swapfile listed with its size. You can also check free -h to see the total memory and swap usage.

  8. Make swap persistent across reboots (Optional but Recommended): The above steps enable swap for the current session. To ensure it’s available after the machine restarts, you need to add an entry to /etc/fstab.

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

How it Works Mechanically

When your application requests more memory than is physically available, and the system’s page cache has been reduced, the Linux kernel’s memory management will start moving less frequently accessed memory pages from RAM to the swap file on disk. This frees up physical RAM for active processes. When those pages are needed again, they are read back from the swap file into RAM, which is significantly slower than accessing RAM directly. Enabling swap turns a hard crash (OOM killer) into a performance degradation.

Tuning Swap Behavior: Swappiness

The swappiness parameter controls how aggressively the kernel swaps memory pages. It’s a value between 0 and 100. A higher value means the kernel will swap more readily, while a lower value means it will try to keep data in RAM for as long as possible.

  • Default: Usually 60.
  • For servers with plenty of RAM: You might set it lower (e.g., 10) to prioritize keeping things in RAM.
  • For memory-constrained machines where swap is a lifeline: You might keep it higher or even set it to 100 to aggressively use swap.

To check current swappiness:

cat /proc/sys/vm/swappiness

To set swappiness temporarily (until reboot):

sudo sysctl vm.swappiness=10

To make it persistent, add it to /etc/sysctl.conf:

echo 'vm.swappiness = 10' | sudo tee -a /etc/sysctl.conf

Then apply the change:

sudo sysctl -p

The next thing you’ll likely encounter is understanding the performance implications of swap usage and how to monitor it effectively.

Want structured learning?

Take the full Fly-io course →