How to add swap space in Linux
Our machines has various important factors which used to work together to work a machine properly and efficiently. These factors could be different from machine to machine and also usage of machine.
Some of these factors are CPU Speed, Amount and speed of Memory, Disk Speed, Cache, Machine cooling mechanism. Today we talk about one important factor which comes in pictures when machine feels that we have less memory and we might h
ave memory issues in near future.
So this concept is called Virtual Memory or Swap. As we said its virtual memory, this is not actual main memory. So this is part of Hard-disk which act as RAM.
Sometime we need to increase the swap file after the Linux installation. This enhances the system performance in case system needs some extra memory for application. In normal case or during installation system makes a partition for s
wapping but from 2.6 Linux Kernel swap file can be used for swapping also having same effect as the partition do. It really gives a great flexibility in administrative work. Today I have to do the same for some of my client. So I just
share this with you people as well.
SO the situation is that one of my client using Zimbra Server as Mail Server after using it for 2 months , he is still using 2 GB RAM on Server. Now Server has around 500 Users. Now sometime system gets hangs when all the RAM get used
up by the Zimbra process. After requesting client many times for increase the Server RAM. Finally I have to increase the swap file of system. I Know this is not best solutions, we should add RAM, but management is not agree to invest
RAM right now and i have to resolve this case for few days till get RAM ordered.
How to make the swap file
For making swap file, we are using dd command.Now i am trying to make 4 GB file. It is like copying from /dev/zero on /swapfile, base count for this 1M count (how many times) 1*4096 = 4GB
[root@mail ~]# dd if=/dev/zero of=/swapfile bs=1M count=4096 4096+0 records in 4096+0 records out 4294967296 bytes (4.3 GB) copied, 68.3321 seconds, 62.9 MB/s
Set up the swap file as swap Linux area, like mkfs in case of partition
[root@mail ~]# mkswap /swapfile Setting up swapspace version 1, size = 4294963 kB
Activate the /swapfile as swap space
[root@mail ~]# swapon /swapfile
You can verify this with
[root@mail ~]# cat /proc/swaps Filename Type Size Used Priority /dev/mapper/VolGroup01-LogVol01 partition 4128760 147852 -1 /swapfile file 4194296 0 - 2 [root@mail ~]# free -m total used free shared buffers cached Mem: 2025 1773 251 0 15 126 -/+ buffers/cache: 1631 394 Swap: 8127 144 7983
For making it permanent or activate at the time of system reboot, add the swap file entry in /etc/fstab file.
/swapfile swap swap defaults 0 0
If you want to remove this first remove the entry from /etc/fstab file, then use
Now remove the /swapfile file, never used to remove the /swap file without using swapoff