48

What specifically can Linux do when it has swap that it can't without swap?

For this question I want to focus on the difference between for example, a Linux PC with 32 GB RAM and no swap vs. a near identical Linux PC with 16 GB RAM with 16 GB swap. Note I am not interested in the "yes, but you could see X improvement if you add swap to the 32 GB PC". That's off-topic for this question.


I first encountered the opinion that adding swap can be better than adding RAM in comments to an earlier problem.

I have of course read through this: Do I need swap space if I have more than enough amount of RAM? and...

  • Answers are mostly focussed on adding swap, for example discussing disk caching where adding RAM would of course also extend the disk cache.
  • There is some mention of defragmentation only being possible with swap, but I can't find evidence to back this up.
  • I see some reference to MAP_NORESERVE for mmap, but this seems a very specific and obscure risk only associated with OOM situations and possibly only private mmap.

Swap is often seen as a cheap way to extend memory or improve performance. But when mass producing embedded Linux devices this is turned on its head...

... In that case swap will wear flash memory, causing it to fail years before the end of warranty. Where doubling the RAM is a couple of extra dollars on the device.

Note that's eMMC flash NOT an SSD!. Typically eMMC flash does not have wearleveling technology meaning it wears MUCH faster than SSDs


There does seem to be a lot of hotly contested opinion on this matter. I am really looking for dry facts on capabilities, not "should you / shouldn't you" opinions.

What can be done with swap which would not also be done by adding RAM?

  • "There are two principle reasons that the existence of swap space is desirable. First, it expands the amount of memory a process may use. Virtual memory and swap space allows a large process to run even if the process is only partially resident. As “old” pages may be swapped out, the amount of memory addressed may easily exceed RAM as demand paging will ensure the pages are reloaded if necessary." Chapter 11: Swap Management, from kernel.org On desktop - not obvious, unless you run a demanding app. But on server . . . – Sergiy Kolodyazhnyy Dec 10 '19 at 06:58
  • 3
    @SergiyKolodyazhnyy thanks for the link! The interesting thing here is that as memory sizes have far outgrown disk Io bandwidth, the usefulness of extending RAM this way appears to be diminishing. I mean with large 4GB swap you can see mistakes where the kernel suddenly has to load 1GB from swap and freezes the box for to do it. But with "small" 0.5GB swap you may only be adding <2% to a 32gb RAM system so the practical extension is seemingly insignificant. – Philip Couling Dec 10 '19 at 08:23
  • @SergiyKolodyazhnyy I agree especially with your "But on server...". And want to add: by comparing 32GB RAM with 16GB RAM + 16 GB Swap, I don't think the swap can be called "desirable". (in general, yes, but not here) –  Dec 10 '19 at 09:33
  • Historically kernel crash dumps were placed in the swap partition, with the best practice being to have twice as much swap as RAM to ensure space for the image dump. While that's not the current case it does point out one thing you can do with swap which can't be done with ram, persistently save the contents of memory across reboots when the filesystem is corrupt or otherwise unavailable. – Matthew Gauthier Dec 10 '19 at 12:25
  • 4
    Can you point more specifically at the comment which states that adding swap is better than adding RAM? I don't think that's ever true... – jcaron Dec 10 '19 at 17:00
  • 2
    @jcaron read carefully here and here. It's also a pretty strongly inferred by stating that having lots of ram also requires swap. Yet confusingly when pressed the explanation is ... The same as that of increased capacity – Philip Couling Dec 10 '19 at 22:48
  • 2
    Interesting edge cases. Would be interesting to see if those kernel heuristics based on swap still exist nowadays. Something that was true 4 years ago isn't necessarily still true. – jcaron Dec 11 '19 at 00:36
  • Swap is typically slow so your system shows signs of distress before the memory fills up. It can therefore warn you. Especially as the OS tries to keep it unused. Some OS data structures might not behave well with much RAM and no swap (this has been a problem with Linux buffer cache), but it's not really a swap property. – eckes Dec 11 '19 at 04:03
  • Your assumption about flash memory is patently false at this point in time. Modern SSD's are rated for hundreds of times their capacity in total writes over their expected lifetime of at most 10 years (usually closer to 3-5 for warranty), and it's very rare unless you have a seriously underprovisioned system to be actively using swap that constantly, because any sensible system only writes out pages that have changed in memory and aren't already in persistent storage. – Austin Hemmelgarn Dec 12 '19 at 02:57
  • 1
    @AustinHemmelgarnas this is not an assumption. I've worn out flash memory quite a few times. You are mixing up between hardware on a PC or server and typical hardware on an embedded device. Flash on a single-board-computer is much cheaper and wears out much faster. A lot of work has been put in by manufacturers to ensure a modern SSD wears evenly. – Philip Couling Dec 12 '19 at 05:29
  • @PhilipCouling _ It's also a pretty strongly inferred by stating that having lots of ram also requires swap._ – That isn't true. A lot of RAM means huge page tables. Page table entries themselves cannot be paged out. – forest Dec 12 '19 at 10:16
  • 1
    @PhilipCouling Oops! You're right, I did misread. – forest Dec 12 '19 at 10:37
  • Have a dramatic slowdown when you use it? :) – rogerdpack Dec 13 '19 at 18:11

14 Answers14

64

Hibernation (or suspend to disk). Real hibernation powers off the system completely, so contents of RAM are lost, and you have to save the state to some persistent storage. AKA Swap. Unlike Windows with hiberfil.sys and pagefile.sys, Linux uses swap space for both over-committed memory and hibernation.

On the other hand, hibernation seems a bit finicky to get to work well on Linux. Whether you "can" actually hibernate is a different thing.

jmullee
  • 631
muru
  • 72,889
  • 2
    @CodeCaster That's the main function of swap, yes. However, at least on Linux, hibernation does work by writing the state to swap (file or partition, can be configured). – muru Dec 10 '19 at 10:44
  • Windows makes that distinction, but I don't think Linux ever has. – muru Dec 10 '19 at 10:56
  • 4
    And (if we are not specific to Linux) dumps. See http://jdebp.uk./FGA/dont-throw-those-paging-files-away.html . – JdeBP Dec 10 '19 at 13:58
  • 2
    The page behind that link is from 2007. – Aganju Dec 10 '19 at 20:54
  • So how does Linux hibernate if the swap space is already full? – user253751 Dec 11 '19 at 11:19
  • @user253751 I'll let you know if I ever get it to work. (On a more serious note, I don't think it can, but adding swap is simple on Linux as long as you have the required disk space.) – muru Dec 11 '19 at 11:48
  • 2
    Linux uses swap space for both over-committed memory Pedantically speaking, "over-committed memory" means there isn't enough swap space for all of it. Memory overcommit could very well be one reason why hibernation on Linux is unreliable, but I haven't really looked into it. – Andrew Henle Dec 11 '19 at 17:07
  • "On the other hand, hibernation seems a bit finicky to get to work well on Linux." - My personal experience is that nowadays it works out of the box on Debian at least. 5 years ago it was more of a hassle :) – marcelm Dec 11 '19 at 18:03
  • 2
    @user253751 As a general rule, it doesn't. In fact, you need more free swap space than your total usable RAM on Linux for hibernation to work reliably (ignoring all the hardware and firmware related issues it has). However, you only need enough space to fit everything that's in virtual memory, which means that as long as total memory usage on the system doesn't exceed total usable RAM plus a bit of extra overhead for the hibernation image metadata, you're usually fine. – Austin Hemmelgarn Dec 12 '19 at 02:57
  • Most PCs where I tied to use hibernation would wake up with black screen, so it definitely is tricky to make it work. Shame, especially for laptops. – Tomáš Zato Dec 12 '19 at 16:26
  • Usually you can hibernate... what you can't (if so) is to get a usable system after resuming. Occasionally, though, you can't actually hibernate (and in these cases also likely suspend to RAM): it's when a process is stuck in Disk Sleep. Unfortunately, it's not too rare in my experience with Linux (happens several times a year). – Ruslan Dec 12 '19 at 20:14
  • @AustinHemmelgam Sounds like it's impossible to make hibernation work reliably, because you could always run a program that uses too much swap space, and then try to hibernate. – user253751 Dec 13 '19 at 10:05
17

What can be done with swap which would not also be done by adding RAM?

This question could actually be reworded into What can be done with non-volatile RAM which would not also be done by adding more volatile RAM? . Just because you happen to dedicate a partition for paging(a dedicated way of interacting with volatile RAM), it does not change the fact it is still a part of a persistent secondary storage medium. Swap partition is also not mandatory for putting a system into hibernation, a "swap file" created on a preexisting partition can be used as well.

In the end, whether you are using a swap partition or a swap-file, what you will be storing are things to be written to or from RAM. If you were to pull the power cord from a system with an enabled swap partition, that swap partition would not get magically erased.

While this swap data would not be read in at your next boot (because the paging file would have entries corresponding to processes that are no longer running), and some distros might take deliberate steps to destroy it either during a proper shutdown or a proper reboot, if someone were to pull out a cord out of a system they would be able to examine that swap partition forensically.


As far as the case of embedded devices you mentioned, Flash, being a type of non-volatile RAM (NVRAM or EEPROM) storage, wears out because its ability to take I/O hits (Flash Cell Endurance represented in terms of number of program/erase cycles) pales in comparison to that of volatile RAM. You literally shave off a layer of oxide every time you perform a write to that location, and eventually there is simply no oxide left to allow for persistent storing of the charge and it literally leaks out before its subsequent reading.

On the other hand, volatile's RAM's survivability is virtually nonexistent (on the order of minutes in ideal experimental conditions) in comparison to flash, if or when you cut off its power source. In the case of volatile RAM there is nothing to stop the leakage of the charge and the corresponding state of the flip-flop (inputs, determining outputs, which then re-determine inputs), aka feedback-controlled latches.

NetIceCat
  • 2,294
  • 1
    I would say that your reworded question is a segnifficantly different question. But +1 for a useful answer anyway. – Philip Couling Dec 10 '19 at 07:47
  • Your comment on flash memory is technically correct, but kind of misrepresents the fact that most modern flash memory that would be used for swap is rated for hundreds of times it's capacity in writes, and the swappers on both Linux and BSD are smart enough to not write out pages that are already in swap (or have a copy on disk), so it's almost never going to be an issue. – Austin Hemmelgarn Dec 12 '19 at 02:59
  • You might be unaware that it's possible to encrypt swap with a temporary key — each time the computer is rebooted, a new key is generated. This, swap contents are irretrievably lost on power loss. I have this on my computer. Of course, this strategy prevents hibernation. – Paddy Landau Dec 13 '19 at 07:35
16

32GB RAM and no swap vs. 16GB ram with 16GB swap.

Asked like that, swap mostly does save money, increasing performance per dollar ratio, maybe also per watt.


But swap still is more than "memory as slow as a disk". It is a temporary storage for memory pages, which can be directly (dirty as they are, no filesystem overhead) loaded into RAM when needed.

Of course a lot depends on the load (the kind of the load), and the idea of swapping can even backfire. That is why there is "swappiness" parameter, besides swapon / swapoff itself, and the discussion about the right size.

From wikipedia I got this statement about "swap" in linux (in "paging" article)

The Linux kernel supports a virtually unlimited number of swap backends (devices or files ...

If multiple swap backends are assigned the same priority, they are used in a round-robin fashion (which is somewhat similar to RAID 0 storage layouts),...

This shows that you can turn swapping into something that makes more sense on the hardware level: a dedicated "scratch drive" would give these swapped out pages a better home. Ideally, a scratch drive should be (very) small but fast and robust.


According to the "new" size rule (square root of Giga), your example should compare:

16 GB RAM + 0 GB Swap+1000 GB Disk

16 GB RAM + 4 GB Swap+ 996 GB Disk

Because what really does not make sense is:

16 GB RAM+0 GB Swap + 1000 GB Disk

12 GB RAM+4 GB Swap + 1000 GB Disk

That would be a swap partition on a tmpfs ("ramdisk") - maybe not even too harmful, but I see no benefit at all here. You can't even hibernate.

(see below for zram, and zswap, though, when you add compression to that)


To understand swap You have to consider the whole system and the average load. And because vm/mm (virtual memory management) is a complex system it is really hard to name a clear advantage. I like the idea of a "smooth" transistion into a overloaded system.

I have 8 GB RAM and no swap. But still I defend the concept, AFA I can understand :-)


I found this redhat citation in one of the OP links. The scenario is an ever increasing memory demand, on 2 GB RAM + 2 GB Swap, if I remember:

... In our case [just illustrated], quite a lot of swap is available, so the time of poor performance is long.

But the alternative is OOM even earlier!

The "time of poor performance" is long, yes, but performance only degrades proportionally to the load. I don't know the context, maybe they just want to warn against a too large swap partition. It sounds anti-swap, but on second look is not.

Then again, for the same reason, I have no swap. I want to know when me and my applications hit the ceiling, and then I will decide if I have to diminuish the load, buy more RAM or activate a partition for swap (I have one or two small partitions ready for that).


I looked up this zram, and then zswap thing: very interesting...:

In comparison, zswap acts as a RAM-based cache for swap devices. This provides zswap with an eviction mechanism for less used swapped pages, which zram lacks.

On the other hand, zram works without any swap device. It makes possible what I said makes no sense, but I did not take compression into account.

My point is this eviction mechanism inherent in "swap". This can be very useful under high load, whether you swap by swapping out or by compressing.

  • Thanks. Just a note that the example you say I should have used would be a different question and has been answered elsewhere. This question is a deliberate like-for-like comparison between adding swap and adding more RAM. – Philip Couling Dec 10 '19 at 08:05
  • 2
    Nothing against your Q at all. I wanted to point out the difficulty of comparing 1-to-1 or "like-for-like", between swap and RAM. My "should" was meant as an answer, not a correction. Maybe I should have written 20GB RAM vs. 16GB RAM + 4 GB swap. But then we are back to "swap does nothing but save money". (Which is the same as the ever-true "Buy more RAM".) –  Dec 10 '19 at 09:25
  • 4
    +1 "[...] swap mostly does save money [...]". Historically, this was the very reason. – rexkogitans Dec 10 '19 at 14:38
  • 3
    @rexkogitans: or from another POV, avoid OOM conditions during normal operation, given hard limits on how much RAM you could put in ancient machines. Just running a multitasking OS on early hardware was a big challenge. – Peter Cordes Dec 10 '19 at 22:36
  • 32GB RAM and no swap except...you can still add more swap to it, now you have "64GB" "memory" if desired, at the click of a button :) – rogerdpack Dec 11 '19 at 23:00
  • Swap on a ramdisk just doesn't work. I mean, Linux will let you do it, but it just wastes memory (static overhead of the ramdisk) and time (spent copying data). You can, however, use a compressed ramdisk (ZRAM device in Linux terms), and that works very well for light swap usage. – Austin Hemmelgarn Dec 12 '19 at 03:01
  • @AustinHemmelgarn Then I don't understand man 2 swapon: EINVAL (swapon()) The indicated path does not contain a valid swap signature or resides on an in-memory filesystem such as tmpfs(5). –  Dec 12 '19 at 08:47
  • @rogerdpack I am afraid your recursive argument is a bit lost here. –  Dec 13 '19 at 07:31
  • I guess you could say that the benefit of swap is that it's resizable without having to change a hardware configuration...dynamic :) – rogerdpack Dec 13 '19 at 18:10
  • @rastafile I undeleted this (and this) since both answers, and especially this one, seem quite good and useful so it's a shame to lose them! – terdon Jul 16 '20 at 20:23
15

For a given total, it's always better to have it as RAM than swap.

There is a difference in behaviour between a noswap system and a swapping system, which may or may not be useful: thrashing. As the swapping system starts to run out of total available virtual memory, it becomes more and more blocked on moving pages back and forth between swap and RAM. This slows down the system. In some circumstances this can allow manual or automated intervention to reduce load and recover the system.

In a noswap system, performance remains relatively level until very high memory usage, at which point the OS starts unloading readonly pages from memory-mapped executables and also has very little room for disk cache. It is likely that the OOM killer will be triggered at this point. So a sudden failure rather than slow degradation.

I agree that swap makes little sense for embedded systems, especially in small embedded Flash devices. The world is full of little Linux consumer routers, lightbulbs etc. with fairly fixed memory usage and no swap.

pjc50
  • 3,026
10

Swap, unlike RAM, can be easily disabled, constrained, slowed, or extended -- a useful feature while developing an applications' memory allocation strategy.

Suppose your kernel sees 4G of RAM and 4G of swap, and the OS and development applications (browser, IDE, etc.) consume 3G. That's 5G free virtual: 1G of real and 4G of swap.

You're developing your new database or game, which wants say 2G to load up its hot resources. So it's going along, sbrk and mmap repeatedly, and with swap on, gets its 2G real by the wonder of the kernel's VM layer. Hooray, your code works well. (And your browser or whatever has been page-relegated to swap hell.)

Now, stop your program. swapoff and re-run your program. This time, while it's going about its sbrk rant, it gets an ENOMEM and your code has to deal with an unhappy path. How does it react? How does it dance in this scenario?

Or mount a USB stick, mkswap and swapon on the USB mount, and increase vm.min_free_kbytes to force the kernel to go even more to that USB stick. Rerun your program. How's your app behaving now with slow mallocing VM?

How about doing all of the above while your app is running? How does it behave once it's running and starts experiencing OOM conditions?

There is no end to the ways you can torture a system running low on free real RAM by tuning your swap subsystem live. And unlike cgroups or rusage, you don't have to shutdown your process to alter the virtual RAM available to it.

You can put these kinds of memory abuses under test, using mocks to simulate ENOMEM or slow brk, and that's an appropriate thing to do because it's stable. But nothing beats live-fire exercise in low memory environments for mining the gold of stochastic processes. Swap lets a tester tweak the kernel's memory resources and characteristics on the fly, easily, in a way that's suitably similar to the real world of low memory. Adding or removing RAM sticks does not provide this value.

bishop
  • 3,209
  • Clarifying your implied point - swap space can be added without stopping the machine. Adding physical ram means a shutdown and poweroff, which will halt any long-running process. By creating some swap you may allow the process to complete. – Criggie Dec 11 '19 at 07:49
  • @Criggie Yes, that is a a particular instance of the general flexibility swap provides for "fiddling" with the quantity and characteristics of a system's virtual memory. – bishop Dec 11 '19 at 10:59
  • 2
    You can do all this using various techniques to limit the available RAM for a process or set of processes, e.g. cgroups. – Stephen Kitt Dec 11 '19 at 11:02
  • @StephenKitt you can use rusage or cgroups to emulate slow malloc? You can use cgroups on BSD? – bishop Dec 11 '19 at 11:12
  • 2
    That's about the only use case for voluntarily using swap instead of RAM. And any decent system designer would make damn sure that the database box has as much RAM as they can afford -- ideally every data the DB touched would stay in RAM forever (well, until the hopefully orderly shutdown respectively background sync). – Peter - Reinstate Monica Dec 11 '19 at 11:20
  • @Peter-ReinstateMonica For production, yes; for development, you also want to make sure your program works for companies that aren't quite as rich as yours. – user253751 Dec 11 '19 at 11:21
  • 2
    Swap won’t result in a slow malloc (but I get your point, you can’t get slow page-in without swap). cgroups was an example, you can do some of this with rlimits on BSD, and there are other ways of limiting the amount of memory available to a process. Also, cgroups can be altered on the fly without restarting processes. – Stephen Kitt Dec 11 '19 at 12:23
  • @StephenKitt All true, each way slightly different, just not the ease of swap shenanigans. With respect to cgroups dynamicism, I don't think the rss limits can be modified on the fly. (Other values can though I am sure.) – bishop Dec 11 '19 at 12:29
  • 1
    Memory limits can be adjusted on the fly with cgroups ;-). I agree that ease of use is in the eye of the beholder, and largely depends on one’s familiarity with the tools! I highly recommend spending time with cgroups though, they are a much finer-grained hammer than swap for this kind of stuff. – Stephen Kitt Dec 11 '19 at 12:50
  • 1
    FWIW, it actually is possible on some systems to add more RAM without needing to power off. It's just really rare on consumer hardware, as it's mostly seen as a feature for high-availability enterprise systems (so that they can avoid the insane times it takes the firmware to POST as much as possible). – Austin Hemmelgarn Dec 12 '19 at 03:04
6

This is a bit of a niche case, and admittedly you could just add more ram, but...

Everyone seems to be assuming that swap means a physical non-volatile storage, like a hard drive or flash drive. Swap is more of a process than a physical location.

Consider the following weird shenanigans:

  1. I have a device with 32GB of RAM and no other storage.
  2. I need to process 42GB of data.
  3. Fortunately said data is in nice separate chunks and highly compressible (perhaps something like a time series that is mostly static), and I don't need to access it all at once, so...
  4. I use zram to set up a compressed, in memory swap partition, which lets me page most of it out when I'm not using it.
  5. Thus letting me fit more data into my limited memory without modifying my program code.

A use for swap (that happens to be in RAM), that is more than just what you get from plain RAM (without a swap partition in it).

While, yes, you could technically just add more ram; this sort of weirdness would means that no matter how much ram you add, you could always wrangle a little more space (so long as you know your data/whatever is suitable).

Alan
  • 161
  • It's not as niche of a case as you might think. I actually use such a setup on most of my systems, with memory set to only allow commiting at most the total swap size plus half of RAM (which is how much the zram device will take up if full), and it works wonderfully for cases where I'm not heavily utilizing swap space (which is most of the time). – Austin Hemmelgarn Dec 12 '19 at 03:02
4

What can be done with swap which would not also be done by adding RAM?

Hibernation requires a swap file, even if swap is not otherwise used.

Other than that, nothing. Swap is a good stopgap if

  • RAM is really expensive or limited compared to disk space

  • The system is a general purpose system under the control of 1 user who may try out unpredictable software and/or workloads from time to time.

  • If OOM scenarios are at all possible and paging to disk is preferable to outright crashing of programs.

What specifically can swap do that RAM can't

Two things:

A. Be there when RAM is full.

B. Hold memory pages that aren't actually being used by processes at a given moment (because they are blocked on some condition) to allow RAM to be used for something else.

If you have enough RAM to satisfy any memory request from all processes that will ever run simultaneously, or don't mind OOM errors, A can be ignored.

Linux uses free RAM to cache block-device reads and writes, but it doesn't have to. RAM is faster than block devices. Allowing Linux to page out things processes aren't using to swap will allow more RAM to be used to cache block-device reads. If you don't care about the speed of your block-device access, which can apply in embedded environments like routers, etc., B can be ignored (and in these situations you don't care about hibernation anyway).

Swap is not a disk cache - it's possible for pages that are in swap to not be associated to a disk file or never be intended to be written to disk.

LawrenceC
  • 10,992
  • Paging often saves me since it brings my system to a crawl and is a nice notification that I need to close some programs rather than having everything run perfectly fine until the system totally locks up and then something is killed. – Qwertie Dec 12 '19 at 05:18
  • 1
    Be there when RAM is full. This year I'm inviting Swap to spend chirstmas with me! –  Dec 12 '19 at 17:18
4

You already have the answer, but your question shows you don't yet appreciate that answer.

The simple answer is, "Be large and cheap (and perhaps, flexible)". You already know it, but you dismiss it too. You shouldn't.

RAM isn't "a couple of extra dollars". Gigabyte for gigabyte, for example, ECC RAM on my server costs £130 for 32 GB (RDIMM 2400 DDR4), or about £4/GB. The SDD I'm using for swap costs about £100-130 per TERABYTE, or about £0.11/GB (Micron/Intel with Power Loss Protection). Server farms can take hundreds to thousands (in many cases) of GB per server, with hundreds of servers.

RAM is also more power hungry - an idle process in RAM requires RAM powering and refresh while on SSD the power use is a lot less. On small embedded devices this can be an issue in its own right.

Wear leveling is also less of an issue than you think on most devices. SSDs last longer than people think, and commercially designed devices will choose an SSD to match their expected use, not the extremely poor ones in some cards.

Last, Swap is more flexible. If your task normally uses 32 GB but briefly and rarely some aspect of it needs 64 GB, with RAM you have to provide 64 regardless. With swap it works on 32 and briefly uses, then discards, swap, for the exceptional times.

Tl;Dr - you're completely missing the economic point.

Stilez
  • 1,261
  • Thanks. This question really isn't asking "should I have swap?" But on the points you've raised I do work with both servers and embedded devices (I love my job). I'm well aware of the cost benefit in servers. On the opposit end of the scale the maths is very different for embedded SBCs. They typically don't have SSDs but cheaper eMMC. As this has no wear leveling there is a risk that swap will hammer just one small region of flash and kill it way ahead of time. The same is true of running a laptop off a USB thumb drive... Except you don't need to pay an engineer to replace it if it does ;-) – Philip Couling Dec 13 '19 at 07:30
2

Swap can save you money. RAM is more expensive than the corresponding amount of disk. The goal is to optimize the cost-performance ratio. Of course, RAM is always better than swap, but it will cost you more and goes to waste if you don't use it all.

The usefulness of swap also depends a lot on the type of workload. For example, if you have a lot of apps open, but actively only use one of them, then the RAM used by the inactive programs can be freed up and instead used by the active process. On the other hand, using a single, memory intensive program that needs more memory than the available amount of RAM is a non-optimal use of swap.

Johan Myréen
  • 13,168
  • 2
    Also note that the Linux kernel "swaps" even if no swap space has been provided in the form of a swap partition (or swap file). The kernel frequently discards memory pages that have not been written to ("clean" pages) and can be read back from the executable file. These pages contain program code and constant data. – Johan Myréen Dec 10 '19 at 07:26
1
... In that case swap will wear flash memory causing it to fail years before the end of warranty. Where doubling the RAM is a couple of extra dollars on the device.

I think you'll find this is false. Firstly, couple of extra dollars won't double any reasonable amount of RAM. Secondly, flash is cheaper tham RAM. Thirdly, swap is used for inactive data. If you have a situation where the swap partition is continuously written to, you have too little RAM and without swap some important process would have crashed.

Besides, random writes to a full SSD (which cause them to fail early) are slow. They are very, very slow indeed. Your SSD won't die an early death if your computer isn't slowed down to a crawl by swapping.

It is far better that your computer slows down markedly when it's running out of memory than that some important process crashes.

Now, what can you do with swap that you can't do with RAM?

  • Hibernation
  • Kernel crash dumps
  • Cheap virtual address space (much cheaper than RAM)
  • Disabling overcommit
juhist
  • 182
  • 2
    You are mixing up full PC with embedded SBCs that I was discussing. SSD hard drives are very expensive compared to typical SBC flash and the allowable re-write count on an SSD is orders of magnitude larger. It's pretty easy to kill flash, it's pretty hard to kill and SSD. Re doubling memory... Check the price difference between various R-pi 4 memory variants. There's really not much in it and that's the retail price not manufacture cost. Doubling from from 2 to 4 GB RAM is really really cheap. Sending out an engineer because a customer's device failed is really really expensive! – Philip Couling Dec 10 '19 at 20:25
  • +1 on the part about SSD vs flash. I've used VMs which ran entirely from a USB3 stick, figuring it was a "poor man's SSD." Performance WAS notably better than running from a spinning disk. I've seen those fail (lost / corrupted data) in a matter of 8 - 9 months. If I put the swap on there, they failed faster. Flash is great for "backups" of data but you don't want it as your "daily driver" storage. SSDs are spec'ed differently are are much more reliable, long-term, which is why people can use SSDs for years at a time without significant failure. – Meower68 Dec 10 '19 at 21:11
  • But how many USB sticks can one buy for the price of an SSD? As long as you transfer contents to a new one before the current fails… – WGroleau Dec 11 '19 at 03:14
  • Virtual address space is always cheap. It doesn't have to be backed by any memory at all. – user253751 Dec 11 '19 at 11:22
  • 1
    I had swap on my ssd for 5 years. Eventually I replaced the SSD for a much bigger one. I don't think wearing out an SSD is a real concern for almost all users. – Qwertie Dec 12 '19 at 05:16
-3

What specifically can Linux do when it has swap that it can't without swap?

Handle lots of idle processes (or documents, images, ...) that you aren't currently interacting with, but which you don't want to explicitly save and then load all over again in a few minutes/hours/days when you need them again.

Whichever apps or pages or images you are actually interacting with, are fully swapped in. The ones that you have reduced to tabs, or just left idle and obscured by a different window, can be swapped out if there's not enough RAM to hold everything in RAM at once. It's also probable that if you reveal an app, it doesn't need to swap in everything, but only the representation of the page on screen, until you start interacting with it.

Disk space (and SSD) is cheap, but slower to access than RAM, and impossibly slow if you want to access it randomly at the level of words rather than in larger chunks. RAM is randomly accessible, but more expensive. Swapping is the removal of large chunks of data from RAM to the swap device, from where it can be retrieved reasonably fast as chunks when needed.

Toby Speight
  • 8,678
nigel222
  • 317
  • 3
    This misses the point of the question. The question is asking if, for a given total storage, there is anything that can be done with swap that can't be done with RAM. – Mark Dec 10 '19 at 19:26
  • @mark if that's the question, there is only one answer. It saves money (at the expense of possibly compromising performance, for my reasons above). – nigel222 Dec 11 '19 at 08:46
-3

I think you rather miss the point of swap. Most motherboards have a fixed number of memory slots, and can handle only limited sizes of memory modules. (Which only come in fixed sizes, anyway.) So if your machine has 16 GB of memory installed, and you suddenly need to run a program that takes 17 GB, what do you do?

If you have a free memory slot and some spare cash, you can order more memory, but even overnight delivery takes time. And if you don't have a free slot, do you order a new motherboard? Or do you just use swap?

jamesqf
  • 185
  • 6
  • 2
    This misses the point of the question. The question is asking if, for a given total storage, there is anything that can be done with swap that can't be done with RAM. – Mark Dec 10 '19 at 19:26
  • @Mark: But that reading of the question is nonsensical. And if it is what the OP meant, then the question really needs to be clarified. – jamesqf Dec 11 '19 at 04:34
  • @Mark There's no point to miss because your question is completely pointless. –  Dec 11 '19 at 21:46
  • 3
    @jamesqf Perhapse reading the whole question might clarify it for you. To paraphrase... there appear to be many with the opinion that swap adds something intrinsic other than adding capacity (see question for links). If that opinion is correct, then I want to know facts about what, other than capacity is added by swap. – Philip Couling Dec 12 '19 at 11:01
-3

I find the premise of the question completely wrong here. Swap exists not to replace RAM and not to “extend” it. Swap partition is a cushion that your butt falls on when your server is running out of available memory. The purpose and functionality of swap pretty well described in the kernel documentation and nowhere it says that it is to “replace RAM”.

  • 2
    The question is explicitly about 32Gb RAM versus 16Gb RAM + 16 Gb swap, i.e. the total amount of available memory is the same. There's no cushion - in case A you'd be out of RAM and in case B you'd be out of RAM and swap – Sergey Dec 12 '19 at 05:23
  • @Sergey when host has 16GB RAM + 16GB swap it still only has 16GB RAM. Swap is not an extension of RAM by any means. It only allows applications to possibly allocate more memory in RAM instead of getting OOMd at very high performance cost. However there is a big BUT - application cannot acquire more memory than available RAM. It is something else other than the application itself that goes into swap. I.e. other application memory while that application is not scheduled to run by kernel scheduler. Application will never see swap as RAM.This is something that’s handled entirely on system level – Alexey Kamenskiy Dec 12 '19 at 05:35
  • 1
    So, you're saying that on a machine with 16Gb of RAM (and some swap) it is impossible for an app to allocate, say, 17Gb of memory and have completely transparent read-write access to that memory as if it was all in RAM? I'm pretty sure what you're describing is not how virtual memory works. – Sergey Dec 12 '19 at 05:46
  • @Sergey virtual memory != RSS. – Alexey Kamenskiy Dec 12 '19 at 06:32
  • It depends on the allocation calls and the Operating system if it will be transparent or not.

    If you "pin" the memory to be in physical RAM it will OOM. Some hardware (such as x86 and x86_64) supports transparent paging, while other hardware, such as z80 does not.

    – Robert Wm Ruedisueli Dec 12 '19 at 17:54
-3

There are three things swap can do that regular RAM can't.

First, TMPFS: Swap buffers (not necessarily swap itself) are used for TMPFS. While you don't need physical swap to use TMPFS, not having swap will limit your TMPFS size to that of physical memory.

Second, Hibernation: Hard drive swap is non-volatile. This means it can survive a reboot allowing for full power-off hibernation.

Third, Size: Let's say you load an insanely large amount of data into a database that requires memory addressing. With Swap you can have that all memory addressed while still being swapped out to disk, allowing for much better random access, and the use of software designed for an in-memory database. While some programs can use a memory mapped file for this, such a program would have to be designed as such.

While the third is typically only found in server and compute environments, the first and second are common in all environments.

  • 1
    tmpfs does not require swap, for Linux at least: https://www.kernel.org/doc/Documentation/filesystems/tmpfs.txt – Reid Dec 12 '19 at 17:43
  • As far as I know memory mapped files don't need swap either. So your insanely large DB example may not be correct. I'm not 100% so if you have evidence to the contrary I'd be interested to see it. – Philip Couling Dec 12 '19 at 19:19
  • TMPFS does not require swap, but it can use swap. Swap is much larger. – Robert Wm Ruedisueli Apr 05 '20 at 12:39
  • I didn't say memory mapped files, I said actual memory addressing. Basically the program isn't designed to use filesystem and needs memory, but it's not really accessing it all that often. I have clarified this. – Robert Wm Ruedisueli Apr 05 '20 at 12:40