Swap file

From LQWiki
Jump to navigation Jump to search

To use swap with Linux can improve performance in situations where memory is low. Typically, you will be prompted to create a swap partition when installing Linux. But this feels very old-school because you cannot change the size of your swap without re-setting up your computer. So you want to create a swap file.

To create a 1GB swap file and use it, issue

dd if=/dev/zero of=/tmp/swap bs=1024 count=1000000
mkswap /tmp/swap  # write a signature so the system knows this is swap
swapon /tmp/swap
cat >> /etc/fstab << EOF
/tmp/swap none swap pri=100,defaults 0 0
EOF

To find out if your swap is in use, issue the command

swapon -s