Tuesday, September 6, 2011

Virtual Memory in Linux- SWAP

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

According to RedHat, the size of swap space can be calculated by using the following formulae.

M = Amount of RAM in GB, and S = Amount of swap in GB, then

If M < 2
S = M *2
Else
S = M + 2

Using this formula, a system with 2 GB of physical RAM would have 4 GB of swap, while one with 3 GB of physical RAM would have 5 GB of swap.

Basically you can create virtual memory by two ways...

1. Create a new partition
2. Use existing partition


Create a new partition

First of all create a new partition.It should be double of RAM in size
Change its ID to 82. 82 is the ID of swap
Now run the following command

[root@localhost ~]# mkswap /dev/sda7 <-------i m assuming the you have created sda7 partition

Note: mkswap format this partition into swap file system

[root@localhost ~]# swapon /dev/sda7

Note: swapon command will activate the swap file system >Now edit your fstab

[root@localhost ~]# vim /etc/fstab

/dev/sda7 swap swap defaults 0 0

save and exit the file and mount it

[root@localhost ~]# mount -a

you can check the status of your swap by the following command

[root@localhost ~]# swapon -s


Use existing partition


Suppose i have a ram of 256 mb and also have a partition /dev/sda6 which has free space space upto 512mb,then i will use this partition for create virtual memory

[root@localhost ~]# dd if = /dev/zero of = swapfile bs = 512 mb count=1


dd -->disk to disk copy,,
if --> input file,,
/dev/zero -->accumlate zero,,
of --> output file,,
swapfile --> it can be any name,,
bs --> block size,,
512 mb --> size of swap ,,
count=1 --number of times command runs,,


>Now run the following commands

[root@localhost ~]# mkswap swapfile

[root@localhost ~]# swapon swapfile

[root@localhost ~]# vim /etc/fstab

/swapfile swap swap defaults 0 0

save and exit the file and mount it

[root@localhost ~]# mount -a

If you wanna unactivate the swap, run following command

[root@localhost ~]# swapoff swapfile

Mount-Unmount in Linux

In Linux, we can not access a device directly. To access a device we need a certain point on our system, from where we can access that device, and that certain point is always a 'directory'. We can access any device by mounting it with a directory.

To mount a device or partition, the mount command is used. We can mount devices temporary or permanently.

Lets see how to mount device temporary:

you can mount a partition with any directory ,Temporary or Permenent.

Temporary mount

The mount command provides large number of options and the common pattern for mount command is shown below.

To mount a partition,run:

[root@localhost ~]# mount /dev/sda4 /abc

To mount cdrom, run:

[root@localhost ~]# mount /dev/cdrom /mnt


To check whether the partition has mounted or not, run following command

[root@localhost ~]# df -Th

if you wanna detach it, run the following command

[root@localhost ~]# umount /abc

Note: After reboot,the partition will detach automatically


Permanent mount

The /etc/fstab and edit it

[root@localhost ~]# vim /etc/fstab

/dev/sda4 /mnt defaults ext3 0 0

save and exit the file

Now run mount command

[root@localhost ~]# mount -a

Now if you wanna detach it again, then you have to remove the above lines from the fstab file and then you have to run the following command

[root@localhost ~]# umount /abc

Note: If you dont wanna detach the partition,then dont remove the entry from the file,,it will still work after reboot. Keep in mind,not to change any other entries of file,bcoz it can lead your system to unbootable condition.