Wednesday, January 8, 2020

What the Swap (space) ?

If the system needs more memory and the RAM is full, inactive pages are moved from memory to swap space (rather than being discarded).

Swap space is portion of hard drive used for virtual memory.

Usually this is a dedicated partition aka swap partition.
It can also be a special file and is used as a swap FS.

Sizing swap space -
 2GB RAM - 4GB swap space
 Say for 250 GB RAM, 10-16GB of swap space is fine.

Create Swap File with dd -

# dd if=/dev/zero of=/swapfile bs=1024 count=5120
# chmod 0600 /swapfile
# mkswap /swapfile
# swapon /swapfile

add entry to /etc/fstab to enable loading the swap file on boot

# vi /etc/fstab
~ /swapfile             swap swap defaults 0 0

# swapon -a

Check status of swap FS -

# swapon -s
# cat /proc/swaps
# free -m

Disable Swap -

# swapoff /swapfile
# swapon -s

Create Swap FS using a partition -

get a partition using disk/parted, say, /dev/sdb

# partprobe /dev/sdb
# mkswap /dev/sdb2
# swapon /dev/sdb2 

# free -m


vm_swappiness

Swappiness is property of linux kernel that changes the balance between swapping out runtime memory as opposed to dropping pages from the system page cache.
Can be set to values between 0-100.

Low value means kernel will try to avoid swapping as much as possible, thereby improving application responsiveness.
Higher value will make kernel swap as aggressively as possible, leaving more memory free for caches.



No comments:

Post a Comment