Managing swap
Written by Simone
Swap File
- To create a 2GB swap file we can use "dd" command like this:
# dd if=/dev/zero of=/mnt/swapfile bs=1024 count=2097152
bs=1024
means read and write up to 1024 bytes at a time and count
it's the size of the file (1024 x 2048)MB
- Then set the appropriate permissions on the file; make it readable only by root user:
# chmod 600 /mnt/swapfile
- Now prepare the file for swap with the
mkswap
command:
# mkswap /mnt/swapfile
- Next, enable the swap file
# swapon /mnt/swapfile
- Afterwards, enable the swap file to be mounted at boot. Edit the /etc/fstab file and add the following new line in it:
/mnt/swapfile swap swap defaults 0 0
You can also disable the swapfile at runtime, any time you want; just make sure it doesn't exceed your available RAM:
# swapoff /mnt/swapfile
Last but not least, this is how to check your swap usage by process:
$ for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | less