April 23, 2009

How to create a virual memory {swap}

A swap file is a space on a hard disk used as the virtual memory extension of a computer's real memory (RAM). Having a swap file allows your computer's operating system to pretend that you have more RAM than you actually do. The least recently used files in RAM can be "swapped out" to your hard disk until they are needed later so that new files can be "swapped in" to RAM. In larger operating systems (such as IBM's OS/390), the units that are moved are called pages and the swapping is called paging.

One advantage of a swap file is that it can be organized as a single contiguous space so that fewer I/O operations are required to read or write a complete file.
In general, Windows and Unix-based operating systems provide a default swap file of a certain size that the user or a system administrator can usually change.Now i will show you how can you create a virtual memory.

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

# mkswap /dev/sda7 <-------i m assuming the you have created sda7 partition

Note: mkswap format this partition into swap file system

# swapon /dev/sda7

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

# vim /etc/fstab

/dev/sda7 swap swap defaults 0 0

> save and exit the file and mount it

# mount -a

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

# 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

# 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

# mkswap swapfile

# swapon swapfile

# vim /etc/fstab

/swapfile swap swap defaults 0 0

> save and exit the file and mount it

#mount -a

> If you wanna unactivate the swap, run following command

# swapoff swapfile

No comments:

Post a Comment