Adding SWAP Memory in Linux

Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the pre-configured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

The following is what you can do to add new swap space to your system:

To list swap partitions and swap files use:

more /proc/swaps

Follow the steps to configure swap space:

Create swap files

cd /var/tmp
dd if=/dev/zero of=swapfile1 bs=1024 count=1048576

This will create the file /var/tmp/swapfile1 of size 1 GB. Using ‘dd’ command ensures that files have no holes.

Create swap area from swap files

/sbin/mkswap -c -v1 /var/tmp/swapfile1

This checks swapfile for bad blocks, then turns it into swap space

Enable swap files for swapping

/sbin/swapon /var/tmp/swapfile1

Verify that system is using the new swap file

more /proc/swaps

You should see the new entries for swap files.

Adding swap to fstab for automatic loading
Add the following line to the end of your /etc/fstab file

/var/tmp/swapfile1       swap  swap   defaults        0 0

That should add your swapfile automatically on every reboot.

Leave a Reply

Your email address will not be published. Required fields are marked *