28

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.

7 Answers 7

16

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.

1
  • 1
    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. Feb 21, 2010 at 15:02
14

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!

9

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.

6
  • yeah! 8 GB is substantial! Jul 31, 2009 at 12:28
  • 5
    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. Jul 31, 2009 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. Jul 31, 2009 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, 2009 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, 2009 at 15:54
3

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).

3
  • Nowadays it works like windows? I didn't know; was away from linux for a long time now. Jul 31, 2009 at 12:25
  • You can also use tuxonice.net
    – voyager
    Sep 20, 2009 at 15:59
  • Swap files have been possible for quite a while now. Swap partitions perform much much better. Feb 21, 2010 at 15:02
3

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: http://en.wikipedia.org/wiki/Binary_prefix 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

1
1

From my personal experience with Linux, I came to know that the conventional formula of 2X the size of RAM suits only older systems with 1 GB - 2 GB of RAM.

However for newer systems, having more Gigs of RAM, I suggest the following

If RAM < 2 GB, then Swap space = 2 x RAM

If RAM = 2 GB - 8 GB, then Swap space = RAM

This will not have any impact on system performance even while running high performance calculations and applications.

Although I recommend to use the original amount of RAM if your memory is atleast 4 GB. That should keep the system stable and efficient.

If RAM = 8 GB - 64 GB, then Swap space = 0.5 x RAM

In this case, I would recommend to use maximum 4 GB of RAM as that is more than enough, except for multimedia applications.

1
  • Your mem-to-swap size function is discontinuous at 2 GB and again at 8 GB. Which means it's "wrong" at least on one size of the discontinuity...
    – einpoklum
    Jan 2, 2018 at 18:55
0

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.

1
  • vague, and not helpful. outdated guide.
    – ahnbizcad
    Jan 30, 2015 at 7:43

You must log in to answer this question.

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