Managing swap

Written by Simone

Swap File

  1. 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

  1. Then set the appropriate permissions on the file; make it readable only by root user:

# chmod 600 /mnt/swapfile

  1. Now prepare the file for swap with the mkswap command:

# mkswap /mnt/swapfile

  1. Next, enable the swap file

# swapon /mnt/swapfile

  1. 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

Comments

  1. Markdown is allowed. HTML is not allowed.