This Post is intended to understand the swap creation, monitoring and extending in Redhat Linux.
Swap space is a restricted amount of physical memory that is allocated for use by the operating system when the amount of physical memory (RAM) is full. If the system needs more memory resources 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. Read more
Recommended System Swap Space
In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. But because the amount of memory in modern systems has increased into the hundreds of gigabytes, it is now recognized that the amount of swap space that a system needs is a function of the memory workload running on that system. However, given that swap space is usually designated at install time, and that it can be difficult to determine beforehand the memory workload of a system, Redhat recommend determining system swap using the following table.
Amount of RAM in the System |
Recommended Amount of Swap Space |
4GB of RAM or less |
a minimum of 2GB of swap space |
4GB to 16GB of RAM |
a minimum of 4GB of swap space |
16GB to 64GB of RAM |
a minimum of 8GB of swap space |
64GB to 256GB of RAM |
a minimum of 16GB of swap space |
256GB to 512GB of RAM |
a minimum of 32GB of swap space |
Note : On most distributions of Linux, it is recommended that you set swap space while installing the operating system
How to Monitor Swap Space
We shall look at different commands and tools that can help you to monitor your swap space usage in your Linux systems as follows
Using the swapon Command
To view all devices marked as swap in the /etc/fstab file you can use the –all option. Though devices that are already working as swap space are skipped
If you want to view a summary of swap space usage by device, use the – summary (swapon –s) option.
[root@nfsserver ~]# swapon –summary
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
[root@nfsserver ~]#
[root@nfsserver ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
Note :- Use –help option to view more options and information.
Using /proc/swaps
The /proc filesystem is a process information pseudo-file system. It actually does not contain ‘real’ files but runtime system information, for example system memory, devices mounted, hardware configuration and many more.
[root@nfsserver ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
[root@nfsserver ~]#
Using ‘free’ Command
The free command is used to display the amount of free and used system memory. Using the free command with -h option, which displays output in a human readable format.
[root@nfsserver ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 674M 6.5G 9.8M 507M 6.7G
Swap: 2.0G 0B 2.0G
[root@nfsserver ~]#
Using top Command
To check swap space usage with the help of ‘top’ command
Using the vmstat Command
This command is used to display information about virtual memory statistics
[root@nfsserver ~]# vmstat
procs ———–memory———- —swap– —–io—- -system– ——cpu—–
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 6791708 2784 516484 0 0 7 0 24 23 0 0 100 0 0
[root@nfsserver ~]#
ADDING SWAP SPACE
Sometimes it is necessary to add more swap space after installation
You have three options: create a new swap partition, create a new swap file, or extend swap on an existing LVM2 logical volume. It is recommended that you extend an existing logical volume
Extending Swap on an LVM2 Logical Volume
To extend an LVM2 swap logical volume(suppose /dev/mapper/centos-swap is our swap volume)
1. Disable swapping for the associated logical volume:
[root@nfsserver ~]# swapoff -v /dev/mapper/centos-swap
swapoff /dev/mapper/centos-swap
[root@nfsserver ~]# swapon -s
[root@nfsserver ~]#
2. Resize the LVM2 logical volume by 256 MB
[root@nfsserver ~]# lvresize /dev/mapper/centos-swap -L +256M
Size of logical volume centos/swap changed from 2.00 GiB (512 extents) to 2.25 GiB (576 extents).
Logical volume centos/swap successfully resized.
[root@nfsserver ~]#
3. Format the new swap space
[root@nfsserver ~]# mkswap /dev/centos/swap
mkswap: /dev/centos/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 2359292 KiB
no label, UUID=5e487401-9ae0-4e1d-adff-2346edfc6244
[root@nfsserver ~]#
4. Enable the extended logical volume
[root@nfsserver ~]# swapon -va
swapon /dev/mapper/centos-swap
swapon: /dev/mapper/centos-swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/centos-swap: pagesize=4096, swapsize=2415919104, devsize=2415919104
[root@nfsserver ~]#
5. Test that the logical volume has been extended properly
[root@nfsserver ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 677M 6.5G 9.8M 507M 6.7G
Swap: 2.2G 0B 2.2G
[root@nfsserver ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2359292 0 -1
[root@nfsserver ~]#
Creating an LVM2 Logical Volume for Swap
To add a swap volume group (suppose /dev/centos/swap2 is the new volume)
1. Create the LVM2 logical volume of size 256 MB
[root@nfsserver ~]# lvcreate centos -n swap2 -L 256M
Logical volume “swap2” created.
[root@nfsserver ~]#
2. Format the new swap space
[root@nfsserver ~]# mkswap /dev/centos/swap2
Setting up swapspace version 1, size = 262140 KiB
no label, UUID=6ea40455-47a0-46bf-844e-ec0ebd4a4e6a
[root@nfsserver ~]#
3. Add the following entry to the /etc/fstab file
/dev/mapper/centos-swap2 swap swap defaults 0 0
4. Enable the extended logical volume
[root@nfsserver ~]# swapon –va
swapon /dev/mapper/centos-swap2
swapon: /dev/mapper/centos-swap2: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/centos-swap2: pagesize=4096, swapsize=268435456, devsize=268435456
[root@nfsserver ~]#
5. Verify the swap space
[root@nfsserver ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
/dev/dm-3 partition 262140 0 -2
Creating a Swap File
To Add a swap file
1. Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
2. At a shell prompt as root, type the following command with count being equal to the desired block size:
[root@nfsserver ~]# dd if=/dev/zero of=/swapfile bs=1024 count=65536
65536+0 records in
65536+0 records out
67108864 bytes (67 MB) copied, 0.0893063 s, 751 MB/s
[root@nfsserver ~]#
[root@nfsserver ~]# ls -ld /swapfile
-rw-r–r–. 1 root root 67108864 May 17 16:38 /swapfile
[root@nfsserver ~]# du -sh /swapfile
64M /swapfile
[root@nfsserver ~]#
3. Change the permissions of the newly created file
[root@nfsserver ~]# chmod 0600 /swapfile
[root@nfsserver ~]#
4. Setup the swap file with the command
[root@nfsserver ~]# mkswap /swapfile
Setting up swapspace version 1, size = 65532 KiB
no label, UUID=8a404550-e8a3-4f2b-9daf-137fc34f7b6d
[root@nfsserver ~]#
5. Edit /etc/fstab and enable the newly added swap space
/swapfile swap swap defaults 0 0
[root@nfsserver ~]# swapon -va
swapon /swapfile
swapon: /swapfile: found swap signature: version 1, page-size 4, same byte order
swapon: /swapfile: pagesize=4096, swapsize=67108864, devsize=67108864
[root@nfsserver ~]#
6. Verify the swap space created.
[root@nfsserver ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
/dev/dm-3 partition 262140 0 -2
/swapfile file 65532 0 -3
[root@nfsserver ~]#
Hope this has helped you ..
Thanks!!!!