Simple trick to increase swap space on ubuntu 14.10/14.04/12.04, Linux using a file

If you are using a ubuntu box or any linux distribution with small amount of RAM, it's probably good idea to increase swap space on your Ubuntu system.
Well, you can increase swap space by using hard disk partition or you can increase it using a simple file. So, what now? Why I need swap space?



What is swap space?

Swap space in linux is used, when the amount of physical memory (RAM) is full. If the system runs out of physical memory, inactive pages in memory are moved to swap space.
The above definition itself helps why you need swap space.
create Swap space using a file on ubuntu


Let's get to the point. How can you create a swap space using a file?

You may also like Install software packages on Ubuntu

You can create it by following simple terminal commands below. This method helpful, if you don't have any extra empty partitions on your hard disk.

dd if=/dev/zero of=swap_file bs=1024 count=1048576

Here 'swap_file' is the name of new swap file, 'bs' is block size and 'count' is desired size in kilo bytes. Here I set block size as 1024 (i.e. one Kilo byte) and count as 1048576 (i.e. 1 GB= 1*1024 MB = 1024*1024 KB = 1048576 KB).

If you want 2GB file convert 2GB into Kilo Bytes.  Hope you got my point.

After creating swap_file, then run below commands.

mkswap swap_file

swapon swap_file

Put the entry in /etc/fstab file like below, to mount swap space automatically.

path/to/swap_file none swap sw 0 0

To check if your swap space is automatically mounted, you can run swapoff -a (it turns off all swap spaces)
then run
swapon -a (it turns on all swap spaces)
To check if your swap space is mounted or not run
swapon -s

How much space you can use as swap space?

 
If your RAM is less than or equal to 1 GB then you can use 2*(RAM space).
For higher RAM spaces it is recommended that use 0.9,0.8, 0.75 of your RAM space
 For example if I have 16 GB RAM, I will create 0.5*16=8 GB swap space. Hope you understand this. I am again mentioning that swap space using partition is recommended than using a file.

Any ideas, improvements or bugs, feel free to contact me.

Let me ask one thing How much space are you guys using on your systems?

Comment below.
Share on Google Plus

About Hari krishna

ThoughtWorker, Computer Science graduate from India. I am passionate programmer, blogger, thinker. I love open-source and linux. I love making friends. Just send me 'hi' to any of my social profiles. I love helping people.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

We love your Feedback