Prompt.cz

A Practical Guide to Linux

User Tools

Site Tools


swap-space-management

Swap Space Management


1. Extending a swap space

1.1. Extending the current swap space

(processes using swap must be stopped first)

1.1.1. View the current memory and swap usage:

# free -h


1.1.2. Display top 10 processes using the swap space including percentage values:

# find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(sed 1d /proc/swaps | awk 'BEGIN{sum=0} {sum=sum+$(NF-2)} END{print sum}')" '{process[$1]=$2; sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") {used_swap=process["VmSwap"]; sub(/[ a-zA-Z]+/,"",used_swap); percent=(used_swap/TOTSWP*100); printf "%10s %-30s %20s %6.2f%\n",process["Pid"],process["Name"],process["VmSwap"],percent}}' '{}' \; | awk '{print $(NF-2),$0}' | sort -hr | head | cut -d " " -f2-


1.1.3. Verify the volume group has enough space available:

# vgs


1.1.4. Deactivate the swap space:

# swapoff /dev/vg00/swap_lv


1.1.5. Extend the logical volume:

# lvresize -L +2048M /dev/vg00/swap_lv


1.1.6. Format the logical volume as swap space:

# mkswap /dev/vg00/swap_lv


1.1.7. Activate the swap space:

# swapon /dev/vg00/swap_lv


1.1.8. View the new memory and swap usage:

# free -h


1.2. Creating an additional swap space

(processes using swap cannot be stopped)

A. Creating a swap logical volume

1. View the current memory and swap usage:

# free -h


2. Verify the volume group has enough space available:

# vgs


3. Create a 2 GB logical volume for swap:

# lvcreate -n swap2_lv -L 2048M vg00


4. Format the logical volume as swap space:

# mkswap /dev/vg00/swap2_lv


5. Activate the swap space:

# swapon /dev/vg00/swap2_lv


6. Edit /etc/fstab to make the swap space available permanently:

# echo '/dev/mapper/vg00-swap2_lv swap swap defaults 0 0' >> /etc/fstab


7. Update systemd with the new /etc/fstab configuration (RHEL/CentOS 7/8):

# systemctl daemon-reload


8. View the new memory and swap usage:

# free -h


B. Creating a swap file

1. View the current memory and swap usage:

# free -h


2. Verify that there is enough free space in the root file system:

# df -h /


3. Create a 2 GB swap file:

# dd if=/dev/zero of=/swapfile bs=1024 count=2097152


4. Format the file as swap space:

# mkswap /swapfile


5. Set up correct file permissions:

# chmod 600 /swapfile


6. Activate the swap space:

# swapon /swapfile


7. Edit /etc/fstab to make the swap space available permanently:

# echo '/swapfile swap swap defaults 0 0' >> /etc/fstab


8. Update systemd with the new /etc/fstab configuration (RHEL/CentOS 7/8):

# systemctl daemon-reload


9. View the new memory and swap usage:

# free -h

2. Configuring the swappiness value

2.1. Display the swappiness value:

# sysctl vm.swappiness
vm.swappiness = 60

or

# cat /proc/sys/vm/swappiness
60


2.2. Permanently change the swappiness to the required value:

# echo 'vm.swappiness=30' >> /etc/sysctl.conf

or

# echo 'vm.swappiness=30' >> /etc/sysctl.d/99-swappiness.conf


2.3. Reload the settings:

# sysctl -p

or

# sysctl -p /etc/sysctl.d/99-swappiness.conf


2.4. Verify the new swappiness value:

# sysctl vm.swappiness
vm.swappiness = 30

3. Removing a swap space

A. Removing a swap logical volume

1. View the current memory and swap usage:

# free -h


2. Deactivate the swap space:

# swapoff /dev/vg00/swap2_lv


3. Remove the swap logical volume:

# lvremove /dev/vg00/swap2_lv


4. Remove the entry from /etc/fstab:

# sed -i '/swap2_lv/d' /etc/fstab


5. Update systemd with the new /etc/fstab configuration (RHEL/CentOS 7/8):

# systemctl daemon-reload


6. View the new memory and swap usage:

# free -h


B. Removing a swap file

1. View the current memory and swap usage:

# free -h


2. Deactivate the swap space:

# swapoff /swapfile


3. Remove the swap file:

# rm -f /swapfile


4. Remove the entry from /etc/fstab:

# sed -i '/swapfile/d' /etc/fstab


5. Update systemd with the new /etc/fstab configuration (RHEL/CentOS 7/8):

# systemctl daemon-reload


6. View the new memory and swap usage:

# free -h