Add Swap space on Centos 7

Server is having memory issues w lots of OOM kill errors, currently there is no swap present on OS. Need to add SWAP space

Section 1 - Add Swap File

check current swap space

swapon -s

check existing disk space to see where you can take disk space from:

df -h

[root@awstestbox rules.d]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/xvda1      8.0G  7.1G  915M  89% /

devtmpfs        460M     0  460M   0% /dev

tmpfs           496M   16K  496M   1% /dev/shm

tmpfs           496M   58M  438M  12% /run

tmpfs           496M     0  496M   0% /sys/fs/cgroup

tmpfs           100M   12K   99M   1% /run/user/42

tmpfs           100M     0  100M   0% /run/user/1000

tmpfs           100M     0  100M   0% /run/user/0

/ partition has 915M available, will create 200MB swap from that partition

[root@awstestbox /]# dd if=/dev/zero of=/swapfile count=200 bs=1MiB

200+0 records in

200+0 records out

if want to use different partition, add the path:  (do not use fallocate !!)

dd if=/dev/zero of=/home/swapfile count=200 bs=1MiB

chmod 600 /swapfile

mkswap /swapfile

swapon /swapfile

add the /swap to fstab

vi /etc/fstab, add

/swapfile               swap           swap     sw  0   0


If swap is a separate block device

lsblk

NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

xvda                                 202:0    0  200G  0 disk 

├─xvda1                              202:1    0  100G  0 part 

└─xvda2                              202:2    0  100G  0 part /

xvdf                                 202:80   0  100G  0 disk 

└─ghe_storage_9624a3ac-ghe_user_data 254:0    0  100G  0 lvm  /data/user

xvdg                                 202:96   0    6G  0 disk 



root@test:/home/admin# fdisk /dev/xvdg

Welcome to fdisk (util-linux 2.33.1).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.


The old swap signature will be removed by a write command.


Device does not contain a recognized partition table.

Created a new DOS disklabel with disk identifier 0x0beba1e0.


Command (m for help): n

Partition type

   p   primary (0 primary, 0 extended, 4 free)

   e   extended (container for logical partitions)

Select (default p): p

Partition number (1-4, default 1): 

First sector (2048-12582911, default 2048): 

Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-12582911, default 12582911): 


Created a new partition 1 of type 'Linux' and of size 6 GiB.


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): p

Disk /dev/xvdg: 6 GiB, 6442450944 bytes, 12582912 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

Disklabel type: dos

Disk identifier: 0x0beba1e0


Device     Boot Start      End  Sectors Size Id Type

/dev/xvdg1       2048 12582911 12580864   6G 82 Linux swap / Solaris

Command: w

mkswap /dev/xvdg1



Section 2 - adjust Swapiness

The swappiness parameter determines how often your system swaps data out of memory to the swap space. This is a value between 0 and 100 that represents the percentage of memory usage that will trigger the use of swap.

With values close to zero, the system will not swap data to the drive unless absolutely necessary. Remember, interactions with the swap file are "expensive" in that they are a lot slower than interactions with memory, and this difference in read and write speed can cause a significant reduction in an application's performance. Telling the system not to rely on the swap as much will generally make your system faster.

Values that are closer to 100 will try to put more data into swap in an effort to keep more memory free. Depending on your applications' memory profile, or what you are using your server for, this might be the better choice in some cases

check current swapiness level

cat /proc/sys/vm/swappiness

add new level

sysctl vm.swappiness=10

add to /etc/sysctl.conf

vm.swappiness = 10

vm.vfs_cache_pressure = 50

check new swap space,

[root@awstestbox /]# free -h

              total        used        free      shared  buff/cache   available

Mem:           990M        650M         76M         57M        262M         99M

Swap:          199M          0B        199M

Section 3 - Remove and relocate swap file

turn off the swap

swapoff -v /swapfile

delete the swap file

rm -f /swapfile

recreate new swap file again (see section 1)