Tuesday, November 3, 2015

Creating Swap space


Swap space is like virtual memory in Windows. In Linux environment it's very important to manage memory in occasions such as OOM(out of memory). When Large programs run simultaneously this can occur if it's a server the case is worst. So we have to assign additional memory space from our hard disk to avoid OOM. But assign memory in hard disk is not a good practice because reading and writing on hard disk is very slow with compared to RAM. So using swap space or virtual memory is more convenient in SSD. Any way here is how to assign swap space on hard disk



open terminal and type:

dd if=/dev/zero of=/swapfile bs=1024 count=524288
in this case we are getting the null file at /dev and create file called swapfile with null of 512MB(bs is the block size that's mean 1024bytes(1KB) of block.for complete 512MB there should 524288 of these blocks.1024*512=524288)

Then type :
mkswap /swapfile
make the swap file called swapfile in root.

type :
chown root:root /swapfile
make own the file to root user in root group

chmod 0600 /swapfilie
make file permission

swapon /swapfile
Active the swap file we created

Now we are completed and now we should check whether it has setup successfully. To check this type as follows

swapon -s
Then you will see a result on terminal as follows

Filename                Type        Size    Used    Priority
/dev/sda5                                  partition    2594812    0    -1
/swapfile                                  file        524284    0    -2
 

You can see here it's taken the swap space just we've created.

You can check whether it's using by system typing following command
free -m
It will result as following
                     total       used       free
Mem:          2997       1154       1842
-/+ buffers/cache:        604       2393
Swap:         2533          0       2533



How to remove Assigned Swap space.

To remove assigned swap space firstly we should off the swap space. To do it type,

swapoff /swapfile

Now type the following to remove the swap file.

sudo rm /swapfile

No comments:

Post a Comment