Ideal size of swap partition in linux

There are no rules of thumb regarding the size of the swap partition, although some ancient ones (like swap=2xRAM) still are seen sometimes in the wild. Ubuntu, like any other Linux distro, will run just fine even without a swap partition, unless you run out of RAM.

It depends solely on your usage of the machine how much swap is needed. On typical modern desktop machines with 4-32GB of RAM usually a swap partition of 1GB is more than sufficient, unless you run really huge workloads. The only exception to this is when you want to use the hibernate/suspend-to-disk feature, in that case you should have a swap partition that is as least as large as physical RAM.

As an example, on my main machine with 16GB of RAM I have a 1GB swap partition and no problems at all with that, while on my laptop with 4GB of RAM I use the hibernate/suspend-to-disk function and therefore use a 4GB swap partition.

Although you can always add swap space if need be:

$ dd if=/dev/zero of=/swap.file bs=1M count=1000
$ mkswap /swap.file
$ swapon /swapfile

Will give you 1.0 GB of swap space on the fly. I like swap space, because of the way I work, but for most people running a lappy or desktop for entertainment or office-productivity applications, the machine will never use swap space.

The earlier rule of thumb for a Swap partition was twice your ram size, example for a system with ram of 2gb, it would be 2×2=4gb, so the swap partition would be of 4gb.

On an ordinary desktop or laptop (not server) system (assuming you are not using hibernate) there is no correlation between the amount of ram you have and the amount of swap space you ought to have.

On ordinary systems, swap use is near zero. Swap is mainly a safety valve for unexpected situations. If almost no swap is used, 2GB makes a good safety valve. In the unlikely event that you notice more than a few hundred MB of swap actually getting used, you ought to increase the swap allocation above 2GB.

I hope this clears up!