How to increase SWAP Memory in Centos 7

To increase SWAP Memory in Centos 7

Swap space is used when the physical memory (RAM) is full.Swap memory increases the efficiency of Ram. This tutorial explains how to increase SWAP memory in Linux after installation.

There are two methods to increase the swap memory after installation.


1. fdisk utility

To check the system for swap information, use swapon -s option.

[root@linuxhelp ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda2                                  partition    2047996    25348    -1

To view the system' s overall memory usage, execute the following command.

[root@linuxhelp ~]# free -m
                      total       used      free     shared    buffers     cached
Mem:                   987         837       149        5          0        191
-/+ buffers/cache:     646         340
Swap:                  1999        24        1975

Before allocating swap, check the space availability of drive using the following command.

[root@linuxhelp ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        15G  8.5G  6.3G  58% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           494M  140K  494M   1% /dev/shm
tmpfs           494M  7.1M  487M   2% /run
tmpfs           494M     0  494M   0% /sys/fs/cgroup
/dev/sda1       297M  106M  192M  36% /boot

Create partition for swap space using the following command.

[root@linuxhelp ~]# fdisk /dev/sda
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.
To create new partition type n press enter
If free space is available, fdisk normally starts the new partition at the first available sector or cylinder. The actual size of the partition depends on disk geometry. Press enter of First cylinder line
Command (m for help): n
Partition type:
p   primary (3 primary, 0 extended, 1 free)
e   extended
Select (default e):    ---------------------------------------->  Press enter select default partition type
Using default response e
Selected partition 4
First sector (35432448-41943039, default 35432448): --------------->  Press enter select default First sector value
Using default value 35432448
Last sector, +sectors or +size{K,M,G} (35432448-41943039, default 41943039):------->  Press enter to select full hard disk space
Using default value 41943039
Partition 4 of type Extended and of size 3.1 GiB is set
Press “ n”  to create a new partition
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (35434496-41943039, default 35434496):
Using default value 35434496
Last sector, +sectors or +size{K,M,G} (35434496-41943039, default 41943039): +100M
Partition 5 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000b1397

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4712447     2048000   82  Linux swap / Solaris
/dev/sda3         4712448    35432447    15360000   83  Linux
/dev/sda4        35432448    41943039     3255296    5  Extended
/dev/sda5        35434496    35639295      102400   83  Linux

Command (m for help): l
0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
4  FAT16 < 32M      40  Venix 80286     85  Linux extended  c7  Syrinx
5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
f  W95 Ext' d (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 < 3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix
Press “ t”  to change the type of system
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' 
Press “  p”  to list the partition
Command (m for help): p
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000b1397
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4712447     2048000   82  Linux swap / Solaris
/dev/sda3         4712448    35432447    15360000   83  Linux
/dev/sda4        35432448    41943039     3255296    5  Extended
/dev/sda5        35434496    35639295      102400   82  Linux swap / Solaris
Press “  w”  to save command
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Then refresh the system settings, using the partprobe command.

[root@linuxhelp ~]# partprobe /dev/sda

Format the swap partition by using mkswap command

[root@linuxhelp ~]# mkswap /dev/sda5
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=3351f2e7-0cca-4d7c-9a11-1ca982d81f03

Activate the swap partition by using the following command.

[root@linuxhelp ~]# swapon /dev/sda5

To verify whether the new swap volume is recognised, run the following command.

[root@linuxhelp ~]# cat /proc/swaps
Filename                Type        Size    Used    Priority
/dev/sda2                               partition    2047996    25312    -1
/dev/sda5                               partition    102396    0    -2

Open the /etc/fstab configuration file.

[root@linuxhelp ~]# vim /etc/fstab

Add the following entry at the end of the file.

/dev/sda5                                 swap                    swap    defaults        0 0

Save and exit the file.

Restart the system.

[root@linuxhelp ~]# reboot &ndash f

After rebooting the system, verify whether swap is working properly or not

[root@linuxhelp ~]# cat /proc/swaps
Filename                Type        Size    Used      Priority
/dev/sda2                               partition    2047996    102396      0            -1
/dev/sda5                               partition    102396    0    2047996         0           -2


2. File method

If no additional disks is available, create a file and use that file for swap space.
Utilize the following command to create a swap file under /root directory.

[root@linuxhelp ~]# dd if=/dev/zero of=/root/myswap bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 24.8354 s, 43.2 MB/s
[root@linuxhelp ~]# ls -l /root/myswap
-rw-r--r--. 1 root root 1073741824 Jul  6 06:32 /root/myswap

Change the permission of the swap file.

[root@linuxhelp ~]# chmod 600 /root/myswap
[root@linuxhelp ~]# ls -l /root/myswap
-rw-------. 1 root root 1073741824 Jul  6 06:32 /root/myswap

Make the file as a swap file using mkswap command.

[root@linuxhelp ~]# mkswap /root/myswap
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=499cf292-9daa-49de-a621-2f9143e59179

Enable the newly created swap file

[root@linuxhelp ~]# swapon /root/myswap

Now add the entry in the /etc/fstab configuration file.

[root@linuxhelp ~]# vim /etc/fstab

Add the following at the end of the file.

/root/myswap                              swap                    swap    defaults        0 0

Save and exit from the file.

Verify whether the newly created swap area is available or not.

[root@linuxhelp ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda5                                  partition    102396    5128    -1
/dev/sda2                                  partition    2047996    0    -2
/root/myswap                               file    1048572    0    -3
[root@linuxhelp ~]# free -m
                        total       used    free     shared    buffers     cached
Mem:                     987        835      151          4          0        323
-/+ buffers/cache:       512        474
Swap:                   3123         5       3118

FAQ
Q
How to Process to Increase Size of Swap Partition and use it for Hibernation?
A
Creating the swap partition,
Activating the swap partition,
Making the new swap partition work for hibernate (optional)
Q
What is Memory consuming programs?
A
Sometimes, a large program (like LibreOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory. A significant number of the pages used by these large programs d
Q
What is the main usage of Hibernation (suspend-to-disk) ?
A
The hibernation feature (suspend-to-disk) writes out the contents of RAM to the swap partition before turning off the machine. Therefore, your swap partition should be at least as big as your
Q
What is the difference between buffered and registered?
A
A buffer is a device used on EDO or Fast Page Mode modules. A register is used on SDRAM modules. Registers and buffers “re-drive?or “amplify?signals as they enter the memory module.
Q
How to Optimizing Swap performance?
A
Because swap space uses a disk device, this can cause performance issues in any system that uses swap space significantly because the system itself may also be using the same disk device at t