Linux Swap management

Before doing any thing about swap we should know what is swap, for what purpose the swap is useful and other stuff related to swap management.
Q. What is a swap or page file or virtual memory?
When we are running applications in Linux their frequently used data is stored in RAM for frequent access. If the applications uses the full RAM there is a possibility of system slowness. The left solution is to increase RAM size so that we can run those applications with out any issue. As the RAM is bit costly buying it is not preferred. So people come with a solution where your own hard-disk can be used as RAM(in fact virtual RAM or called as swap) it self so that RAM data which is not frequently accessed can swapped to HDD swap location for feature access.
Q. Tell about memory hierarchy?
When your machine is processing data it uses some memory hierarchy to swap the memory for effective processing the data and applications. Below is the memory hierachy up to Swap this swapping take place.
  1. CPU registers(hundred of bytes)
  2. L1 cache(in KB’s)
  3. L2 cache(in MB’s)
  4. L3 cache(in 100′s of MB’s)
  5. RAM (in GB’s)
  6. Swap(1.5 times of RAM)
  7. HDD(1000′s of GB’s)
  8. SAN/NAS(in TB’s)
  9. Tape Drives(100′s TB’s)
Q.Why we require a swap file, with out swap can a system exits?
Swap management is required for proper working of machine if the RAM size is less. With out Swap we can run a machine if we have enough RAM.
Q. Why is swap size is less than or equal to 1.5 times the RAM?
 This is kind of thumb rule. If we add up memory from CPU registers to till RAM that will accounts to 1.5 times the RAM. In other words if your processes swaps entire this memory to Swap partitions its easy to take that running applications to their memory locations for proper running of applications.

Crating Swap in Linux

Step1: First check what is the swap space the system is having and utilization of swap size. To view swap details in Linux execute below commands.
#free -m   
#swapon -s 
-s for stats
#cat /proc/swaps
this will give output of swap statistics in Megabytes(m), if we want the output in Kilobytes then free -k
Step2: Before starting the Swap creation/modifications we have to take precautions such as switch off all the swap and no user should be logged in

#swapoff -a

Step3: Check in the system if there is any raw space in the system
#fdisk -l
Step4: If the system is having free space then create a partition which support swap(partition type 82) with required amount of free space
#fdisk /dev/hda
p                #press p to print the partition table
n                #press n for creating new partition
256M          #specify the amount of swap required
t                 #press t to change the partition type to 82 (because partition type 82 is well supported for swap)
8                 #enter the partition no on which u want to create swap(here i am creating on /dev/hda8 partition)
82               #specifying the partition type
p                 #press p to print the partition table and to just conform the /dev/hda8 partition type
w                #press w for writing the changes to partition table (if suppose if u have any problem or trouble just                                           press q to quit from fdisk utility without any problem)

Step5: update the partition table changes to kernel so that there is no need to restart the system/server
#partprob
Step6: Permanently mounting the partition details,in order to do this one we have to update the /etc/fstab file
#vi /etc/fstab
/dev/hda8    swap    swap    defaults    0 0
enter the above entires in to fstab file,save it and exit from editing /etc/fstab file
:wq
Step7: Formatting/creating swap signature on the newly created partition
#mkswap /dev/hda8
Note: stpe6 and step7 are interchangeable.
Step8: update the mount table to kernel
#mount -a
Step9: now on the swap so that it can be available for use.
#swapon -a
Step10:check weather the swap is updated or not
#free -m   
#swapon -s 
#cat /proc/swaps

Removing swap:

Step1: Before doing any thing with swap first we have to switch of the swap
#swapoff -a
Step2: Remove/comment the entry of swap from /etc/fstab file
#vi /etc/fstab
then save and exit
Step3: update the kernel about mount table changes
#mount -a
Step4: Remove the partition used by swap
#fdisk /dev/hda8
d                #press d to delete the partition
8                #specify the partition no to be deleted
w                #press w to write the changes to partition table and quit the fdisk utility
Step5: update the partition table changes to kernel with out rebooting the system/server
#partprobe
Step6: Now on the swap
#swapon -a
Step7: Now check weather swap is updated properly or not
#free -m   
#swapon -s 
#cat /proc/swaps