How to add Swap Space in Centos 7.x?

First, let's understand what swap space is. In a Linux machine, swap space is used when the physical memory on the same machine is full. In case, your Linux machine memory is full, the inactive pages on the memory can move to the swap space. However, you can’t consider swap as a replacement for physical memory because it can help with very small amounts of memory. 

Recommended System Swap Space

RAM in the system

Swap Space Recommendation

Recommended swap space if hibernation is allowed

⩽ 2 GB

2 times the amount of RAM

3 times the amount of RAM

> 2 GB – 8 GB

Equal to the amount of RAM

2 times the amount of RAM

> 8 GB – 64 GB

At least 4 GB

1.5 times the amount of RAM

> 64 GB

At least 4 GB

Hibernation not recommended


Source : https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace#swap-creating-file

We can use free disk space to create a new partition. Please refer to the following steps to set up Swap Space on Centos.

  1. Run the below fstab command.

    fdisk /dev/vdb
  2. Type n for the new partition and after that type p for the primary partition and give the partition number. Here, we will give 1 as the partition number.

    fdisk /dev/vdb
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0x5a4d90a3.
    
    Command (m for help): n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-8388607, default 2048): 2048
    Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607): 4388607
    
    Partition 1 of type Linux and of size 4 GiB is set
    
    Command (m for help): t
    Selected partition 1
    Hex code (type L to list all codes): 82
    Changed type of partition 'Linux' to 'Linux swap / Solaris'
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
  3. Run command mkswap against the device/partition that we have created earlier using fdisk/parted. You can type -L optionally that can be used to set LABEL on the swap partition.

    mkswap -L swap1 /dev/vdb
    Setting up swapspace version 1, size = 4194300 KiB
    LABEL=swap1, UUID=bd5f0cf7-cc5f-4ad2-a952-a60d3267124f
  4. Let us modify /etc/fstab file inorder to mount a new swap device on boot.

    vi /etc/fstab
    /dev/vdb               swap       swap      defaults        0 0
  5. Run the below command to enable swap device.

    swapon -s
    Filename			Type		Size	        Used	Priority
    /dev/vdb                      partition	       4194300	          0	    -2
  6. Finally check with using free-m command.

    [[email protected] ~]# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           3947         639         127         236        3180        2790
    Swap:          4095           7        4088

Looking to remove your exiting swap space. Please refer to Accuweb > Remove Swap Space article for more details.

Related Articles

I am having an upload error. What is maximum upload limit in Shared Hosting?

Linux Shared Hosting   In Linux Shared Hosting, we have restricted the upload size limit to 2...

How to Display Actual Error in Magento Website?

You may have seen following errors when Magento do not work properly for some reason. These...

How to Repair MySQL Database from cPanel WHM and phpMyadmin

This article describes how to repair MySQL database using cPanel, WHM and phpMyAdmin. Repair...

Where do I upload my files/folders in Shared Hosting plans?

All the files associated with your website must be placed inside the root folder of your domain....

How to change Database Collation in MySQL Server from phpMyAdmin?

Collation is a configuration level setting in the MySQL. It directs how the character data will...

  • 0 Users Found This Useful

Was this answer helpful?