Skip to content

Setup Swap Space

What is Swap Space?

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. But because the amount of memory in modern systems has increased into the hundreds of gigabytes, it is now recognized that the amount of swap space that a system needs is a function of the memory workload running on that system. However, given that swap space is usually designated at install time, and that it can be difficult to determine beforehand the memory workload of a system, we recommend determining system swap using the following table.

Swap Space Recommendations

Amount of RAM in the System Recommended Amount of Swap Space
4GB of RAM or less a minimum of 2GB of swap space
4GB to 16GB of RAM a minimum of 4GB of swap space
16GB to 64GB of RAM a minimum of 8GB of swap space
64GB to 256GB of RAM a minimum of 16GB of swap space
256GB to 512GB of RAM a minimum of 32GB of swap space

How to create and enable the swapfile

  1. Create the /swapfile.

    Create /swapfile
    sudo fallocate -l 1G /swapfile
    
  2. Change Permissions of the /swapfile.

    Change Permissions
    sudo chmod 600 /swapfile
    
  3. Use the mkswap command on /swapfile.

    Make Swap
    sudo mkswap /swapfile
    
  4. Enable the /swapfile.

    Enable Swap
    sudo swapon /swapfile
    
  5. Allow the /swapfile to be remounted on reboot.

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