Wednesday, June 1, 2011

SWAP Memory (Creating Swap file)

What is swap?

Swap space is an area on disk that temporarily holds a process memory image. When physical memory demand is sufficiently low, process memory images are brought back into physical memory from the swap area on disk. Having sufficient swap space enables the system to keep some physical memory free at all times.

There are three types of swap: device swap, file system swap and pseudo swap. Device swap is a separate partition allocated when the disk is configured. File system swap allows you to use the existing file system to allocate swap space. Pseudo swap is allocated from system memory on some systems.

How can I create swap on these machines?

Though we cannot create device swap, we can still create create file system swap. Steps are given below to create swap file:log-in as root and check your existing mounted partitions to create swap file using disk usage utility :

[root@shah-lp ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda1 9.9G 7.8G 1.6G 84% /

none 7.6G 0 7.6G 0% /dev/shm

/dev/sdb1 414G 32G 362G 8% /share

As we can see above /dev/sdb1 is mounted at /share and it got enough space to create swap file.

Assume my machine got 4GB physical RAM & 2GB of SWAP. Now we are looking to increase it to 8GB, 6GB more. follow below steps:

[root@shah-lp ~]# dd if=/dev/zero of=/share/swapfile bs=1G count=5

5+0 records in

5+0 records out

5368709120 bytes (5.4 GB) copied, 64.3235 seconds, 83.5 MB/s

[root@shah-lp ~]# mkswap /share/swapfile

[root@shah-lp ~]# swapon /share/swapfile

[root@shah-lp ~]# echo " /share/swapfile swap swap defaults 0 0" >> /etc/fstab

[root@shah-lp ~]# mount -a

To confirm run :

[root@shah-lp ~]# free -m

total used free shared buffers cached

Mem: 15367 11428 3938 0 53 10378

-/+ buffers/cache: 996 14370

Swap: 23551 0 23551

[root@shah-lp ~]# swapon -s

total used free shared buffers cached

Mem: 15367 11428 3938 0 53 10378

-/+ buffers/cache: 996 14370

Swap: 23551 0 23551



No comments: