How to setup SWAP on CentOS 7?

How to setup SWAP on CentOS 7?

This tutorial explains how to setup SWAP on CentOS 7.

SWAP or swap space in Linux is virtual memory, which is used when the amount of physical memory (RAM) is fully used up. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. Swap space can help machines with a small amount of RAM. However, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a significantly slower access time than physical memory. When your system runs out of free physical memory (RAM), your application might start crashing if there is no swap space allocated on your machine. Using swap space will allow your applications to continue running, however, intense swap space usage may drastically reduce your server performance.

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. 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.


  • RHEL / CentOS Linux 6, 7 OS.


  1. Recommended System Swap Space
  2. Create a swap file.
  3. Enable swapping.


1. Recommended System Swap Space.

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


2. Setup swap.

Let’s say our CentOS 7 machine has 8 GB of RAM. We will setup the minimum recommended 4 GB file for swapping.

First, allocate 4 GB of disk space to our swap file.

Command: fallocate -l 4G /swap_space

Then, modify the permissions so that only the owner of the file (in this case root) can use the file for swapping.

Command: chmod 600 /swap_space

Now, we need to setup the swap file on our system using the mkswap command.

Command: mkswap /swap_space


3. Enable swapping.

All that is let now is to enable our swap file using the swapon command. Use the swapoff command to disable the swap file or swapoff -a to disable all swap devices.

Command: swapon /swap_space

Command: swapoff /swap_space


NOTE: To enable our swap file boot time, edit /etc/fstab by including the following line at the end of the file:

/swap_space swap swap defaults 0 0


You can check if your newly assigned swap file enabled by using the swapon -s, free -h or cat /proc/swaps commands.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.