In this article, we intend to teach you How To Add Swap on Centos 7. Let’s see what is exactly Swap space means.
Introduction to Swap Space
A computer has a sufficient amount of physical memory but most of the time we need more so we swap some memory on disk. Swap space is a space on a hard disk that is a substitute for physical memory. It is used as virtual memory which contains process memory images. Whenever our computer runs short of physical memory it uses its virtual memory and stores information in memory on disk. Swap space helps the computer’s operating system in pretending that it has more RAM than it actually has. It is also called a swap file. This interchange of data between virtual memory and real memory is called swapping and space on disk is “swap space”.
Virtual memory is a combination of RAM and disk space that running processes can use. Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full.
Swap space can be useful to computers in various ways:
- It can be used as a single contiguous memory which reduces I/O operations to read or write a file.
- Applications that are not used or are used less can be kept in a swap file.
- Having sufficient swap files helps the system keep some physical memory free all the time.
- The space in physical memory which has been freed due to swap space can be used by OS for some other important tasks.
In this guide, we will show how to create and enable a swap file on Centos 7.
How To Add Swap Space on Centos 7
Check Server Storage for Swap on Centos 7
swapon, a general-purpose swap utility.
Note: If nothing is returned by the command, then the summary was empty and no swap file exists.
Another way is with the
free utility, which shows us the system’s overall memory usage. You can see our current memory and swap usage (in megabytes) by typing:
Output total used free shared buffers cached Mem: 3953 315 3637 8 11 107 -/+ buffers/cache: 196 3756 Swap: 0 0 4095
As you can see, our total swap space in the system is 0. This matches what we saw with
At this point, you should check your available storage space on your Centos 7 server. To do this, you can use the following command:
Output Filesystem Size Used Avail Use% Mounted on /dev/vda1 59G 1.5G 55G 3% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 8.3M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
Create a Swap File on Centos 7
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB
ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Jul 25 11:00 /swapfile
Enable Swap File on Centos 7
sudo chmod 600 /swapfile
ls -lh /swapfile
-rw------- 1 root root 4.0G Jul 25 11:00 /swapfile
sudo mkswap /swapfile
Output Setting up swapspace version 1, size = 4194300 KiB no label, UUID=b99230bb-21af-47bc-8c37-de41129c39bf
sudo swapon /swapfile
Output Filename Type Size Used Priority /swapfile file 4194300 0 -1
Also, you can use the
free utility again to corroborate your findings:
Output total used free shared buffers cached Mem: 3953 315 3637 8 11 107 -/+ buffers/cache: 196 3756 Swap: 4095 0 4095
At this point, your swap file is enabled at the moment, but when you reboot, the server will not automatically enable the file for use. You can change that by modifying the
fstab file, which is a table that manages filesystems and partitions.
Edit the file with
sudo privileges in your text editor:
sudo vi /etc/fstab
At the bottom of the file, you need to add a line that will tell the operating system to automatically use the swap file that you created:
/swapfile swap swap sw 0 0
When you are finished adding the line, you can save and close the file. The server will check this file on each bootup, so the swap file will be ready for use from now on.