Managing Swap Space

Create and Format a partition for swap space.

A swap space is an area of a disk which can be used with the Linux kernel memory management subsystem. Swap space in Linux is used when the amount of physical memory(i.e RAM) is full. If the system needs more memory resource 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.

Basic Rule for the Size of SWAP:

As per the Centos documents the basic rule for the SWAP size are,

if the size of the RAM is less than or equal to 2 GB, then size of SWAP= 2 X RAM SIZE.
if the size of the RAM is more than 2GB, then size of SWAP= 2GB + SIZE OF THE RAM.

Swap Space is  recommended to be created at the time of installation. But, additional swap spaces can be created and deleted at any point of time,when it required.  Sometimes we need to increase the size if swap space, so we create additional swap spaces which will be added to the existing swap space to increase the size.

I already covered the details of how to create disk partitions using fdisk and gdisk, Refer below link.

Managing MBR Partitions and File Systems Using fdisk

Managing GTP Partitions and File Systems Using gdisk

Create a swap space

1) Create normal partition using fdisk.

Execute the fdisk command and specify the disk device name as shown below. Use “n” to request a new partition and specify the partition type.

#fdisk /dev/sdd

[root@server1 ~]# fdisk /dev/sdd
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.


Command (m for help): n
Partition type:
   p   primary (0 primary, 1 extended, 3 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (4096-10485759, default 4096): 4096
Last sector, +sectors or +size{K,M,G} (4096-10485759, default 10485759): +2G
Partition 5 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdd: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x51c6e6e7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048    10485759     5241856    5  Extended
/dev/sdd5            4096     4198399     2097152   83  Linux

Command (m for help): 

Note: Using extended partition, I create the logical partition (i.e /dev/sdd5).

2.) Define partition type.

Enter the “t” command to change a partition type then enter the hex code for the new partition type.

Command (m for help): t
Partition number (1,5, default 5): 5
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/sdd: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x51c6e6e7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048    10485759     5241856    5  Extended
/dev/sdd5            4096     4198399     2097152   82  Linux swap / Solaris

Command (m for help):

Note: “L” is used to list all codes.

3.) Save partition tables changes.

“w” command is used to finalize creation request.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server1 ~]#

4.) Updating the new partition table without reboot.

[root@server1 ~]# partprobe /dev/sdd
[root@server1 ~]#

5.) Format the swap partition with swap file system.

#mkswap /dev/sdd5 (used to set up a Linux swap area).

[root@server1 ~]# mkswap /dev/sdd5
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=89b811d9-b4cc-4dbc-9102-3d1a0ec5ad78
[root@server1 ~]#

As shown above, the swap partition is formated with swap file system.

6.)  Turn on newly created swap space and verify it.

To turn on the swap space the syntax is #swapon /dev/sdd5

[root@server1 ~]# swapon /dev/sdd5
[root@server1 ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda2                                  partition    2097148    0    -1
/dev/sdd5                                  partition    2097148    0    -2
[root@server1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            993          88         781           6         123         773
Swap:          4095           0        4095
[root@server1 ~]#

Note: Its shows two swap spaces, one Swap Space is created at the time of installation and now we have created additional swap space(/dev/sdd5).

7.) Make the entry of newly created swap partition on /etc/fstab file.

Here we have to update the mounting details on /etc/fstab file, so that it will be mounted even after the system reboot.

#
# /etc/fstab
# Created by anaconda on Sat Sep 17 02:26:32 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root                         /                       xfs     defaults        0 0
UUID=107451a1-3675-46bf-9074-dd9fd19ca2f9       /boot                   xfs     defaults        0 0
/dev/mapper/centos-home                         /home                   xfs     defaults        0 0
/dev/mapper/centos-var                          /var                    xfs     defaults        0 0
UUID=0fc8a8f0-641c-42fd-8f06-34e40adb15ea       swap                    swap    defaults        0 0
UUID="a6dc4f16-35fc-42a6-8d03-7ccb73df203f"     /Softwarebackup         xfs     defaults        0 0
UUID=89b811d9-b4cc-4dbc-9102-3d1a0ec5ad78       swap                    swap    defaults        0 0
~
~
!wq

Removing the swap partition

Now we have learnt how to create and format the swap partition. Let’s see how to remove the swap partition.

8.)  Deactivate the swap partition.

#swapoff <device name>

[root@server1 ~]# swapoff /dev/sdd5
[root@server1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            993          87         781           6         124         774
Swap:          2047           0        2047
[root@server1 ~]#

9.) Remove the entry from /etc/fstab file.

We remove the entry from fstab file.

#
# /etc/fstab
# Created by anaconda on Sat Sep 17 02:26:32 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root                         /                       xfs     defaults        0 0
UUID=107451a1-3675-46bf-9074-dd9fd19ca2f9       /boot                   xfs     defaults        0 0
/dev/mapper/centos-home                         /home                   xfs     defaults        0 0
/dev/mapper/centos-var                          /var                    xfs     defaults        0 0
UUID=0fc8a8f0-641c-42fd-8f06-34e40adb15ea       swap                    swap    defaults        0 0
UUID="a6dc4f16-35fc-42a6-8d03-7ccb73df203f"     /Softwarebackup         xfs     defaults        0 0

10.) Delete the partition through fdisk and and last inform the kernel to re-read that partition table.

Use “d” to delete a partition and specify the device name, in our case it is 5. Then we have  save the partition table changes using “w” option and last inform the kernel to re-read that partition table with “partprobe” command.

[root@server1 ~]# fdisk /dev/sdd 
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.


Command (m for help): d
Partition number (1,5, default 5): 5
Partition 5 is deleted

Command (m for help): p

Disk /dev/sdd: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x51c6e6e7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048    10485759     5241856    5  Extended

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server1 ~]# partprobe /dev/sdd
[root@server1 ~]#

Important Note: if we have two swap space, in this case the first activated swap space will be used until it is full, then the kernel will start using the second swap.  Using “swapon -s” command displayed the space partitions and can be set with the “pri= “ mount option. if the swap space have the same priority the kernel will write  to them round-robin instead of writing to a single swap space until it is at capacity.

Leave a Reply

4 Comments on "Managing Swap Space"

Notify of
avatar
Sort by:   newest | oldest | most voted
Glenn Matthys
Guest
I guess the article is kind of OK but it leaves out the calculations for systems that need to hibernate and the swap size rule for most systems is ridiculous. If you have a system with 32 GB RAM that means I need to allocate 34 GB of swap space? These days I even run most systems without swap. Also, swap space is not compulsory during installation. It will complain about not having swap space, but allow you to continue nevertheless. Furthermore, depending on the swapiness value, the kernel might move pages into swap even if there is enough free… Read more »
anil chowda
Guest

Hi Gaurav Kumar,

For the beginners stage these articular are more useful.

I have a small query, What is the use of SWAP Memory & why SWAP memory was created most of the database related server.

wpDiscuz