Adding Swap Space
Here are the steps for adding a SWAP space (they are checked for Red Hat distributions).
The example given below is for a machine that has 256 MB of RAM and 512 MB of SWAP. Following the formula for SWAP (SWAP= max(6, 3*RAM)), we have to add 5.5 GB of SWAP space.
Addition of the SWAP space proceeds via creation of a special swapfile.
a) Check the current SWAP space
[~]$ free -m
Total used free shared buffers cached
Mem: 249 245 3 0 1 87
-/+ buffers/cache: 156 92
Swap: 509 148 361
b) Check how much space is occupied by the partitions
[~]$ df -h
Filesystem Size Used Avail Use% Mounted on /dev/hda2 15G 750M 13G 6% / /dev/hda5 4.8G 4.0G 508M 89% /usr /dev/hda3 12G 237M 11G 3% /opt /dev/hda6 24G 4.0G 19G 18% /home /dev/hda1 99M 16M 78M 18% /boot tmpfs 125M 0 125M 0% /dev/shm
One sees that the “/home “ directory has 19 GB of available space. We are going to create the swapfile in there.
1. Get root privileges
[~]$ su -
2. Create a “swapfile” in “/home”
[~]# dd if=/dev/zero of=/home/swapfile bs=55M count=100
bs is the block size of the swapfile in MB (in our case 55) which has to be multiplied by count value (in our case 100) to give desired additional volume of SWAP space (5.5 GB in our case).
3. Set up the swapspace
[~]# mkswap /home/swapfile
Setting up swapspace version 1, size = 5767163 kB
4. Enable the swapfile:
[~]# swapon /home/swapfile
5. See the original and added SWAP spaces
[~]# cat /proc/swaps
Filename Type Size Used Priority /dev/hda7 partition 522072 0 -1 /home/swapfile file 5631992 0 -2
or the RAM and whole SWAP spaces
[~]# free -m
total used free shared buffers cached
Mem: 249 245 4 0 7 183
/+ buffers/cache: 54 194
Swap: 6009 0 6009
In order the system ‘memorizes’ the added SWAP space, open /etc/fstab file and add the following line:
/home/swapfile swap swap defaults 0 0
This enables swap file at boot time.
To remove added swapspace do the following:
1. Get root privileges
[~]$ su -
2. Disable swapfile
[~]# swapoff /home/swapfile
3. Remove /home/swapfile file
[~]# rm /home/swapfile
4. Remove the following line from /etc/fstab file:
/home/swapfile swap swap defaults 0 0
