Swap partition size on a 64 GB RAM computer for memory-intensive work

36

8

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.

wrwt

Posted 2014-07-07T11:59:50.100

Reputation: 475

1Instead 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 – 2014-07-08T04:51:20.003

5The 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. – John WH Smith – 2014-07-08T14:03:03.897

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 – 2014-07-08T17:06:40.580

2It'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 – 2014-07-08T18:11:21.173

SSD's newer than 2013 are quite reliable now. Putting a swap file on them doesn't seem to wear them out any faster than a regular spinning disk. I've got a 2 year old drive with a swap on it and it's still going well. And I have much less RAM. – Matt H – 2014-07-09T01:49:53.247

You could also setup zRam. Give it a higher priority than the disc based swap and you could mitigate swapping to disc a little bit longer.

– fho – 2014-07-10T09:01:34.480

Are you going to hibernate (supend-to.disk) your machine? – Reinstate Monica - M. Schröder – 2014-07-11T11:11:08.577

1If 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 – 2014-08-06T14:01:36.310

Answers

36

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

David Schwartz

Posted 2014-07-07T11:59:50.100

Reputation: 58 310

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 – 2014-07-07T16:04:20.687

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 – 2014-07-07T20:35:43.647

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.) – David Schwartz – 2014-07-07T20:45:52.540

5Linux 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 – 2014-07-08T11:48:07.180

@JasonC - other option is just put it in LVM and don't bother with such details as 'where to put partition to resize it later'. – Maciej Piechotka – 2014-07-08T16:51:37.117

1@MaciejPiechotka Or use a file. – Jason C – 2014-07-08T18:13:24.687

@Ruslan When this answer was written, the OS hadn't been disclosed. In any event, the second point is still correct. Linux will overcommit but it will also refuse allocations it doesn't need to refuse. (Just a bit later, plus you also may find your existing processes getting killed. So it's actually, at least potentially, worse.) – David Schwartz – 2014-07-08T18:16:58.807

On my Debian, if I got it correctly, swap is used to store RAM contents when hibernating to HDD. I think this is a big reason to have swap at least the size of RAM. But in this scenario, hibernation is probably not a common use case. – Palec – 2014-07-09T12:21:22.657

Sorry my English is not good enough, what does this mean: You need swap to get information that will likely never be accessed out of RAM to free up more space for disk cache. – Koray Tugay – 2014-07-10T09:38:15.923

@KorayTugay When your computer first starts up, lots of services load. They allocate and dirty a bunch of memory as they read configuration, load libraries, and so on. Many of those services will never run again because they're things you aren't using. The operating system moves that junk to swap to free up precious physical memory to increase the size of the disk cache which helps reduce I/O and improve performance. – David Schwartz – 2014-07-10T21:24:27.267

29

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.

Bert

Posted 2014-07-07T11:59:50.100

Reputation: 1 476

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. – Dan is Fiddling by Firelight – 2014-07-07T15:01:34.447

12X 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. – David Schwartz – 2014-07-08T17:15:09.653

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
    

Kaz

Posted 2014-07-07T11:59:50.100

Reputation: 2 277

2+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 – 2014-07-07T20:33:38.137

@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 – 2014-07-07T23:12:12.900

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.

kaste

Posted 2014-07-07T11:59:50.100

Reputation: 31

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 – 2014-07-08T00:25:18.020

@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 – 2014-07-10T08:37:53.197

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.

Thorbjørn Ravn Andersen

Posted 2014-07-07T11:59:50.100

Reputation: 291

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.

Eric Towers

Posted 2014-07-07T11:59:50.100

Reputation: 151

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.

ArmanX

Posted 2014-07-07T11:59:50.100

Reputation: 127

3+1 for not swapping to SSD, -1 for swapping to a component that has a very short life span when used like that. – Aviator45003 – 2014-07-07T20:06:16.373

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 – 2014-07-08T17:09:28.217

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 – 2014-07-08T18:15:18.287

The logic is flawed: if the thumbdrive is indeed as fast as SSD, why is it cheaper? – Agent_L – 2014-07-08T18:51:25.100

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 – 2014-07-09T14:32:35.130

@ArmanX you're much more unlikely to wear our a SSD compared to a flash drive, and if you did the odds are you'd have worn out 5-10 flash drives by the time you wore out the SSD. – NPSF3000 – 2014-07-10T08:52:40.010

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.

Damon

Posted 2014-07-07T11:59:50.100

Reputation: 4 002