Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm planning to install Ubuntu 9.04 version. How much space should I dedicate for swap partition. I remember setting it to double the RAM size when I installed long back. At that time my RAM was 256 MB. So I had set the swap partition to 512 MB.

share|improve this question
up vote 14 down vote accepted

I would say a good rule of thumb is indeed just as was mentioned above. 2 times the physical memory. Something to consider here, while it is possible to use a smaller swap partition, and it will suffice under most normal circumstances, if you want this system to be rock solid stable, I would indeed follow the 8 GB recommendation. In fact I recommend 2 * RAM + 1 MB so that there is absolutely room to swap out 2 entire copies of memory. This avoids the "shell game" scenario which can have negative performance repercussions. What this will do for you is guarantee a level of resiliency should you encounter an extraordinary event with your system.

I've seen scenarios where applications behave badly in unattended environments and before you know it, your system starts slowing down to a crawl.

Depending on what you are doing, you might even be able to dispense with the swap file entirely. The extra space for the OS is handy when running many applications at once. However if you only intend to run a few processes, do not intend to interact with the GUI disabling the swap file might be appropriate.

But if you are going to have a swap file I always use the sizing formula below.

[(2 x  RAM) + 1 MB] = Swap File Size

I also recommend putting your swap file on a seperate disk whenver possible as this will increase performance as the OS can swap in and out at the same time as read/writes from the data disk.

I hope this is helpful.

share|improve this answer
You keep saying swap file: this is very rare with Linux, typically swap is a separate partition of the drive, and not a file within another partition. – Justin Smith Feb 21 '10 at 15:02

Linux can operate quite happily with no swap if you have enough RAM for everything you ask it to do, though it is still a good idea to have some swap as it can sometimes gain better performance when close to filling memory by pushing some not-recently-used stuff out to disk freeing up RAM to use as cache/buffer for currently active IO operations.

Also, having some swap, no matter how much RAM you have, does allow a little extra safety net should an app misbehave and need more RAM than usual, or you choose to do something unusual that needs a lot of memory.

In general I don't agree with 2xRAM on modern machines. If you have enough going on to end up with 8Gb worth of pages to swapped out to disk then your system is likely to have ground to a completely unusable speed by that point anyway. Even 1xRAM can be overkill in many cases IMO. Remember that you can add swap space in the form of a file at any time, so you can add more swap later if you find you need to. for example, to add an extra GB of swap:

dd if=/dev/zero of=/path/to/newfile bs=1024 count=1048576
mkswap /path/to/newfile
swapon /path/to/newfile

and to remove it (once no longer needed):

swapoff /path/to/newfile
rm /path/to/newfile

(remember though that swapping to files is a little slower than direct to to a partition or logical volume as you have filesystem overhead and greater potential for fragmentation to consider)

Hibernation, i.e. on laptops/netbooks, may complicate things a little as IIRC Linux stores memory state in the swap space, so you definitely need more then your RAM size as swap if you plan to use hibernation (and I don't know if that needs to be partition/volume based swap or if file based can be used - it will certainly need to be something mentioned in fstab that can be seen very early in the boot process whic the inital ram disk is in use and your real root not yet mounted).

These points may all be moot though. If you have terabytes of disk storage, allocating 8Gb to swap is likely to be very little hardship!

share|improve this answer

According to the Ubuntu SwapFAQ here the calculation is 2*MB of RAM. So in this case 8GB. Realistically however you should not need more then 2GB.

share|improve this answer
yeah! 8 GB is substantial! – darthvader Jul 31 '09 at 12:28
Double the RAM size seems excessive, especially with larger amounts of RAM -- I have 8GB, and I see that working better with a 1GB swap instead of a 16GB swap, since there's already so much headroom in main memory. – Andrew Scagnelli Jul 31 '09 at 12:43
@A Correct - The reason I mentioned that realistic you will not need more then 2GB. The calculations for this is old. However most of the articles online is older then 2 years. – BinaryMisfit Jul 31 '09 at 13:10
It was used to be said that you never needed a bigger swap than 300MB. Now, with 4GB of RAM I have two 1GB swap partitions in two different disks that I never use (setted swappiness to 10). – voyager Sep 20 '09 at 15:52
Noted that I don't use hibernation/suspend on my desktop. (Even then you could make a script to create a swap file before hibernation, and then delete it wen waking up). – voyager Sep 20 '09 at 15:54

Is it still necessary to reserve a partition for swapping? I thought nowadays this also works on a file on the system partition. And then I suppose you can easily change the size if you would insert more memory or if you think you don't need that much swap space.

Edit: Here's how that can be done (starting from linux 2.6 kernel).

share|improve this answer
Nowadays it works like windows? I didn't know; was away from linux for a long time now. – darthvader Jul 31 '09 at 12:25
You can also use – voyager Sep 20 '09 at 15:59
Swap files have been possible for quite a while now. Swap partitions perform much much better. – Justin Smith Feb 21 '10 at 15:02

first of all, if you want to use hibernation etc, you'll need a swap partition at least the size of your RAM.

secondly, remember that RAM is sized in multiples of 2 and not multiples of 10, so declaring 1GB of space for 2 sticks 512MiB of RAM (each 536870912 bytes) is not enough, as this is only 1000MB (1000000000 bytes) and not 1024MiB (1073741824 bytes) as required... instead you'll need to take a calculator, and do 1024 * 1024 * 1024 * # of GiB

read also: and man 8 units on your linux box

so, for four GB of RAM your swap needs to be at least 4294967296 bytes or 4295 MB to use all the features.

as this is pretty big, it probaly should be enough for all your needs

share|improve this answer
You don't need a swap partition to suspend. You can simply use a swap file (which doesn't need to exist until you suspend).… – voyager Sep 20 '09 at 15:57

The Ubuntu SwapFaq will probably answer a lot of your questions. With larger amounts of RAM you may benefit from changing the default "swappiness" as outlined in this article.

share|improve this answer
vague, and not helpful. outdated guide. – ahnbizcad Jan 30 '15 at 7:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .