42

I have 64 GB RAM and 240 GB SSD on my computer, which I'm going to use for memory-intensive calculations (machine learning, data mining, etc.). Most of the advice I found on the Internet are about 2-4-8 GB RAM computers, and they recommend 2x the size of the RAM swap partition (so 128 GB).

Is it reasonable to make a 128 GB swap partition? what advantages do I get by making a huge swap partition?

Do I understand correctly that, in case I run out of physical RAM:

  1. If I have no swap, I get an 'out of memory' error
  2. If I do have swap, some of RAM pages will be copied to swap partition, and the program will continue to run (although more slowly).

Some people say it's a bad idea to make swap on SSD, since it has limited amount of read/write cycles. How fast using swap will it exhaust SSD read/write cycles (as far as I know, it's about 50000 write IOPS)?

I'm using Linux (Ubuntu 14.04 (Trusty Tahr)).

Going to set a 16 GB swap for now, as it should be surely enough (for example, RedHat suggests 4 GB), and 16 GB of disk space isn't actually a big deal.

8
  • 1
    Instead of relying on swap explicitly, if you know the size of your working set up front, or are willing to do a bit more low-level memory management, consider using mmap to allocate your working-set pages. Then your amount of swap will be exactly the amount you need for your process.
    – fluffy
    Commented Jul 8, 2014 at 4:51
  • 5
    The advice recommending "twice the amount of RAM" dates back to old times, when computers had little RAM. Several docs states that it is primarily applicable to computers with < 2GB RAM. Above that, swap size is mostly related to what you're doing with the machine. Commented Jul 8, 2014 at 14:03
  • See also this Server Fault q&a - if you're running Java (and possibly other apps), you want to make sure you have enough swap for them to increase their memory allocations. I personally stick with the RHEL standard of RAM+2 for my swap partition
    – warren
    Commented Jul 8, 2014 at 17:06
  • 2
    It's a shame most of the comments here were removed. Adding back in: It's worth mentioning, incidentally, that if your kernel supports it you may wish to mount your swap partition with discard on an SSD. Also (and this was mentioned in an answer below), don't forget you can use a file instead of a partition for potentially easier management (and no performance hits on an SSD due to e.g. fragmentation).
    – Jason C
    Commented Jul 8, 2014 at 18:11
  • 1
    If you have a memory intensive application, like SVM learning, and you run out of ram and start swapping, everything will become too sluggish to recover and your only available move is going to be pulling the plug (that happened to me a couple time). You probably want your process to be OOM-killed if it start swapping, so that you can at least change stuff and start again. Maybe with SSD drives it's not so bad though. I would check the OOMK settings too - it happened to me on Ubuntu that sometimes processes got OOM-killed when there was still plenty ram left because they allocated aggressively
    – pqnet
    Commented Aug 6, 2014 at 14:01

8 Answers 8

37

You probably only need a small amount of swap. When you have sufficient RAM for your computer's typical working set, which I'm pretty sure you do, you only need swap for two things:

  1. You need swap to get information that will likely never be accessed out of RAM to free up more space for disk cache. Many applications run on system startup and will never be accessed again. You don't want any pages they dirtied stuck in RAM forever. So you need swap to hold them.

  2. You need swap to cover allocations that will never be filled. This space simply has to be available, even though it will not be used. Without it, the system will have to refuse to allocate memory even when it has plenty of free physical RAM because it has insufficient backing store to permit all of its allocations to be used at once.

Neither of these requires a large amount of swap. 16GB, for example, should be more than enough. The purpose is not to let you run bigger working sets at the cost of speed. The purpose is to let you use your 64GB effectively and not have to clog it with junk or reserve it for edge cases that will never happen.

(I agree with Bert that 4GB is quite likely to be sufficient.)

10
  • 2
    @wrwt Put your swap partition at the end of the drive (or at least after your data partition), it will make resizing it quicker and less write-intensive should you ever choose to adjust it (more specifically it will make resizing the data partition to accommodate it simpler, since you don't have to move the start). There is no link between position and performance on SSDs as there sometimes is on mechanical drives.
    – Jason C
    Commented Jul 7, 2014 at 16:04
  • While this answer probably suffice for most hobbyist, then it is bad advice for real servers -- the answer depends on how the application is written, because running out of swap space will cause the out-of-memory killer to kick in and terminate a process by random (yes you read that right; "random")
    – Soren
    Commented Jul 7, 2014 at 20:35
  • 3
    @Soren This is superuser, not serverfault. ;) It's certainly true that setting the swap space is not the only decision you need to make for "real servers". You also need to make decisions about things like overcommit, you may need to tune the OOM killer, and so on. (And the answers get much more complicated if you expect your working set to exceed physical RAM. But almost nobody operates machines that way anymore.) Commented Jul 7, 2014 at 20:45
  • 5
    Linux will overcommit memory even with no swap enabled. It just will OOMK processes when too much memory is actually used. So second point of the answer is wrong.
    – Ruslan
    Commented Jul 8, 2014 at 11:48
  • 1
    @MaciejPiechotka Or use a file.
    – Jason C
    Commented Jul 8, 2014 at 18:13
31

RedHat recommends 4 GB on a machine with 64 GB.

However, sizing swap is more of an art than a science. It depends on what the machine is being used for, how much disk space and memory you have, and other factors. Remember, you can always add more swap later.

Using the 2X physical memory rule is outdated with the amount of memory systems have these days. But running with zero swap is not recommended unless you know what you are doing. The recommendation of 4 GB is a good starting point.

2
  • 12
    +1 for the last paragraph. The 2x recommendation dates back to when most computers didn't have enough ram to avoid swapping in normal use. Subjectively, from using computers then, the 2x limit appears to've been selected as a number big enough that the computer will become unusably slow before running out of swap. Commented Jul 7, 2014 at 15:01
  • 2
    2X also worked fine when systems had 4GB/8GB of RAM and hundreds of gigabytes of disk space. Sure, it was probably more than was needed, but what was the harm? But now that systems have 16GB/64GB of RAM and 128GB/256GB SSDs, the harm is obvious. Commented Jul 8, 2014 at 17:15
12

On Linux, you need enough swap so that the total virtual memory available (RAM + SWAP) is enough for all the processes you want to run at once and their maximum virtual footprint.

If you have less swap than this, or no swap at all, then the following situation happens: the system runs out of memory trying to allocate a page. But, this is still a soft failure even though there is no swap, because the system has plenty of "victim" pages that can be removed in order to make room: namely, the pages of all file-backed memory mappings, such as executables and shared libraries!

As your system demands more and more space for data (which cannot be swapped out), it will increasingly evacuate the executable code (shared libraries and executables), leading to terrible thrashing, as the working set is trimmed into a tighter and tighter set of pages.

Swap space softens this problem by providing a place for anonymous (not file mapped) pages to be swapped out: the pages used for memory allocations, so that executable code can stay in memory.

Even so, if you don't frequently run memory-intensive tasks, you may be able to get away with running swapless most of the time, and manually configure a swap file (instead of a dedicated partition) when you need it. To make a swap file on the fly, become root and:

dd if=/dev/zero of=/path/to/swapfile size=$((1024 * 1024)) count=32768  # 32 Gb.
mkswap /path/to/swapfile
swapon /path/to/swapfile

When you don't need it any more:

swapoff /path/to/swapfile
rm /path/to/swapfile

Notes:

  1. You definitely do not need to configure at least as much swap as you have RAM. This rule of thumb dates back to operating systems where it was a hard requirement due to the way swapping was designed.

  2. There are ways to make Linux fail hard when no memory is available, namely via manipulating the values of these sysctl entries:

    vm.overcommit_memory
    vm.overcommit_ratio
    
2
  • 3
    +1 for actually refering to kernel configuration parameters -- The key is in the part of the question If I have no swap, I get an 'out of memory' error -- which is false -- the truth is that when you run out of swap space the out-of-memory-killer will kick in and kill a random process to free up space -- so the amount of swap space needed depends on how your application is written.
    – Soren
    Commented Jul 7, 2014 at 20:33
  • @Soren It's likely that most of the RAM will be filled with actual data, so the out-of-memory killer won't make much difference. Thx for 'the truth' anyway.
    – wrwt
    Commented Jul 7, 2014 at 23:12
3

There are more considerations. If you need/want suspend to work then you need at least the size of your RAM and then some. However it sounds unlikely you need it given that you seem to mainly build a computational work horse.

In this case please consider using a swap file instead of a partition. You don't need to worry about sizing much, getting rid of or adding it later doesn't require any repartitioning. There is no (noticable) performance penalty using a file over a partition. If you ever happen to need it, have a look at the size and this will then also give you good hints.

2
  • 1
    @Kaz I think you're talking about something different than kaste is. kaste is saying that if you want to be able to suspend/hibernate your computer, turn it off, and pick up where you left off later, you need enough swap space to store all your RAM (else where would it go?).
    – amalloy
    Commented Jul 8, 2014 at 0:25
  • @amalloy If you stored your RAM in your swap space, where does the swap go (remembering that swap is treated as memory - if it lost it is the same as losing ram).
    – NPSF3000
    Commented Jul 10, 2014 at 8:37
2

The workload you want to apply to the machine needs a certain amount of memory to run (remember to add enough to the equation to handle peak loads), and you need to configure your computer to have at least that.

Modern operating systems provide virtual memory as a combination of physical memory and swap space, so if you need more memory than the machine has available you must add enough swap space to fill the gap. I.e. if you need 80 GB max, and the machine has 64 GB you need 16 GB swap.

Typically operating system installers ask you to make an initial swap area (as this is the simplest and allow even tiny computers to install), and it has been found that a good rule of thumb for typical Unix operations is to have virtual memory sized to be three times the physical memory, so this is typically suggested. You, however, know more about the usage pattern so you can change this as appropriate.

There is nothing wrong with working without swapspace if your memory pressure always is less. Linux will transparently use any unused memory as a disk cache.

2

A much better idea than having "a lot of swap" is (re-)organizing your work so that the working sets fit in memory, then using the file-system to store and retrieve the work you do. I.e., instead of forcing the OS to guess what your memory usage patterns will be, use what you know about your problems to control your memory usage patterns.

As a random example that is immediately relevant to me this summer... In implementing the quadratic sieve, one needs a large (apparently) contiguous array to mark up (with some complicated algorithm whose details actually don't matter for this example). The array needs to be ~100 Giga-entries, so easily in the 1 TB range. I could pretend to allocate that and let the OS do an amazing amount of inefficient swapping to get pages in and out of RAM to support all the sequential writes through the array. Instead of doing something that boneheaded, I have arranged to allocate a much smaller array that exactly fits in memory and then reuse that little array to iteratively cover the rest of the big array in slices. I've also stripped the OS, stripped the running set of services, replaced the shell, and customized two layers of memory allocators to do their darnedest to keep as much of the address space available to my process as close to contiguous as possible.

SSD may be fast, but it is not nearly as fast as organizing your computation to do the same set of operations without ever stalling to swap.

1

As the others mentioned, a swap partition is a good idea even if you have plenty of RAM. It's not a good idea to put it on an SSD; the frequent writes of a swap partition will eventually wear out your drive.

If you have a spare USB 3.0 port, I would suggest using a flash drive for your swap space. There are plenty of high-speed flash drives that are as fast as your SSD, but much cheaper - cheap enough to replace if it did begin to fail. A quick search on Amazon shows that there are many decent 16 GB USB 3.0 flash drives for under $20, and even some 64 GB drives under $60.

Partition the entire flash drive as swap space, and you'll have swap space if you need it, and peace of mind knowing that the memory being repeatedly written is easily (and cheaply) replaceable.

6
  • 3
    +1 for not swapping to SSD, -1 for swapping to a component that has a very short life span when used like that. Commented Jul 7, 2014 at 20:06
  • 1
    @T.C. is right, ArmanX - if you're trying to avoid using flash (SSD), why would you use flash on USB? That's irrational.
    – warren
    Commented Jul 8, 2014 at 17:09
  • 2
    @T.C.: Not using SSD for swap because of wearing down the medium is an unjustified urban legend. Swapping does not happen "all the time", but rarely. Also, this is something that has been extensively researched at Microsoft after the Win7 release with the result that the typical access patterns of swapping are quite acceptable for SSD (that's Windows, not Ubuntu, but it's likely that Linux does not perform much worse). You have a hundred (or thousand) times more write operations wearing down your SSD due to silly log files that nobody is ever reading (usually syncing every line).
    – Damon
    Commented Jul 8, 2014 at 18:15
  • The logic is flawed: if the thumbdrive is indeed as fast as SSD, why is it cheaper?
    – Agent_L
    Commented Jul 8, 2014 at 18:51
  • It's a balance. On the one hand, it's a good idea to have a swap file. On the other hand, if a swap file does wear down an SSD (which, you're right, it probably won't matter), it would be better to use a cheap replacement - and a flash drive for $20 is much better than an SSD for more than $100.
    – ArmanX
    Commented Jul 9, 2014 at 14:32
1

You will be fine even with 1GiB (and likely less) of swap. My work computer typically uses no more than 140-150 MiB. A gigabyte is plenty of over-provisioning for that.
Unless you do compute tasks that require datasets in the hundreds of gigabytes and (this one is important!) data is accessed in a more or less access-once fashion, you will never want to have a swap much larger than that. But then again, simply memory mapping a datafile works equally well for that application.

But more swap helps more, right? More of anything is always better!

Consider what difference a swap of, say, 16GiB will make (or think of 64GiB). If you never use these 16GiB, you could as well not have them set aside in the first place. But if you do use them, what happens? Disk, compared to main memory, is exceedingly slow. Even with a SATA-600 SSD, transferring 16GiB takes between 30 and 40 seconds, and 2-4 times as long on some other configurations.
Now someone will inevitably object that you are rather paging in and out a dozen or so 4kiB pages, not 16GiB in one go. While that is true, the point nevertheless stands. If you only need to swap in and out a couple of pages, you don't need 16GiB of swap, but if you do need 16GiB of swap, then you are going to transfer them, too (one way or another).

In theory, 99.9% of all users could even use a 64GiB machine (or any 8+GiB machine) without any swap, and most likely never notice something missing. However, this is not adviseable.
First, it is sub-optimal because the operating system has fewer choices in what it can discard when it runs out of physical memory. There are two things it can do: Swap out something that isn't used, or throw away pages from the buffer cache. If you have no swap, there is only one thing it can do. Throwing away pages from the buffer cache is harmless, but it may noticeably impact performance.
Second, private anonymous mappings might simply fail if there is no swap. That usually won't happen, but eventually when there is not enough physical memory available to satisfy them all, and there is no swap, the operating system has only either this choice, except...
Third, the dreaded OOM killer may kick in. Which means a more or less random process gets to get killed. No thank you. This is not something you want to have happening.

With that said, advice such as you need a swap X times the amount of RAM installed comes from people who repeat something they heard (and didn't understand!) from someone who repeated something they heard (and didn't understand!) decades ago.
The "use 2X your RAM" rule was an easy to remember rule of thumb in the 1980s and 1990s, it was never the "golden truth" (just something that worked OK for most users), and it doesn't apply at all nowadays.

You should have a reasonable amount of swap which you can easily afford (say, a gigabyte), so the OS can page out some stale stuff, and so the world doesn't immediately end when you once ask for a little more memory. But that's it.

You must log in to answer this question.

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