Swap is a virtual memory which is used when there is not much physical memory left in the server. Swap is very important as when server required more memory than it is physically available then swap is used. If swap is not available and server goes out of memory we will see server hangs out, or dies during the middle of process.
Even swap is very important to have in the server the main down side is, since its a hard disk space its read and write time is slow than the actual physical memory, so more the swapping occurs more slower the system will be.
There are no proper rules on how much swap memory you should create but i follow 1 :3 so if you have 2 GB of memory then 6 GB of swap space is idle to create. Depending on the kernel of the linux file larger than 1 GB is slow to read and write so multiple of 1GB swap file must be created for better performance. Swap files never be created in tmp as tmp is ment to deleted on timely manner so its aways better to have separate disk mount or else in the root of the system.
STEP 1:
Find out existing swaps file: You can either one of the following command to find out swap.
more /proc/swaps
OR
swapon -s
STEP 2:
Creating the swap:
i am creating swap file with 1 gb space.
dd if=/dev/zero of=myswap1 bs=1024 count=1048576
STEP 3: Turn each swap file into a swap area /sbin/mkswap -c -v1 myswap1 STEP 4: Enable the swap files for swapping /sbin/swapon myswap1
Swap file is created and activated.
You can check this by running command
swapon -s OR
more /proc/swaps
you must see myswap1 file there.
Final Step :
put the swap in the fstab so that next time you reboot it will run automatically.
so Vi /etc/fstab and append below line in the file and save :wq
/myswap1 none swap sw 0 0