How To Add/Create Swap File

by lifeLinux on May 14, 2011

If you need additional swap space to improve your system performance. You have two different ways: add/upgrade a swap partition or create a swap file. But changing the size or create a new swap partition is not easy, the best solution is to create a new swap file. This articles help you add/create swap file very quickly.

Creating swap file on Linux

Login as root and type the following command to create 512MB swap file

[[email protected] ~]# dd if=/dev/zero of=/swapfile bs=1024 count=524288

if=/dev/zero: read from /dev/zero instead of stdin
of=/swapfile: write to /swapfile instead of stdout
bs=1024: blocksize = 1024 bytes
count=524288: number of blocks are 524288

Setup the swap file with the command: mkswap. Type following command to setup your swap file:

[[email protected] ~]# mkswap /swapfile

To enable the swap file immediately but not automatically at boot time

[[email protected] ~]# swapon /swapfile

To activate the new swap file automatically at the boot, you need to edit the file /etc/fstab, enter

[[email protected] ~]# vi /etc/fstab

Add the following line at end of the file

/swapfile         swap             swap        defaults         0   0

To verify the swap file is working fine with these commands:

[[email protected] ~]# free -m


[[email protected] ~]# cat /proc/swaps

Recommended size of linux swap

1 GB RAM —> 2 GB of Swap file
2 GB RAM —> 4 GB of Swap file
4 GB RAM —> 8 GB of Swap file
8 GB RAM —> 12 GB of Swap file
16 GB RAM —> 24 GB of Swap file
32 GB RAM —> 32 GB of Swap file

Related Posts:

Previous post:

Next post: