Linux Swap

Adding Swap to CentOS 7, RHEL 7, and Oracle Linux 7

1. Check if the System has a Swap partition
# swapon -s

If nothing is returned, then no swap exists.

free -m” is another way of checking for swap.

# free -m
        total       used       free     shared    buffers     cached
Mem:    2815        393       2421          2         19        124
-/+ buffers/cache:        249       2566

2. Check Storage Space
#df -h

3. Create a Swap File
Example: 4GB swap file called /swap

# fallocate -l 4G /swap
# chmod 600 /swap
# mkswap /swap

4. Verify the Swap file:

# swapon -s
Filename	Type		Size	Used	Priority
/swap           partition	4194300	0	-1

5. Make the Swap Permanent
Add /swap to /etc/fstab

/swap   swap    swap    sw  0   0

6. Tweak the Swap Settings

Swappiness:
# cat /proc/sys/vm/swappiness

The default value is 30

Set it to 10
# sysctl vm.swappiness=10

To make the swappiness permanent, edit /etc/sysctl.conf and add the setting:
vm.swappiness = 10

Cache Pressure:
# cat /proc/sys/vm/vfs_cache_pressure

The default is 100, which means the system removes the inode info from the cache too quickly, set it to 50
# sysctl vm.vfs_cache_pressure=50

Make the cache pressure setting permanent in /etc/sysctl.conf by adding:
vm.vfs_cache_pressure = 50