Follow Slashdot stories on Twitter

 



Forgot your password?
typodupeerror
Hardware Linux

Linux Swap Table Code Shows The Potential For Huge Performance Gains (phoronix.com) 79

A new set of 27 Linux kernel patches introduces a "Swap Tables" mechanism aimed at enhancing virtual memory management. As Phoronix's Michael Larabel reports, "the hope is for lower memory use, higher performance, dynamic swap allocation and growth, greater extensibility, and other improvements over the existing swap code within the Linux kernel." From the report: Engineer Kairui Song with Tencent posted the Swap Table patch series today for implementing the design ideas discussed in recent months by kernel developers. The results are very exciting so let's get straight to it: "With this series, swap subsystem will have a ~20-30% performance gain from basic sequential swap to heavy workloads, for both 4K and mTHP folios. The idle memory usage is already much lower, the average memory consumption is still the same or will also be even lower (with further works). And this enables many more future optimizations, with better defined swap operations." "The patches also clean-up and address various historical issues with the SWAP subsystem," notes Larabel.

Context: In Linux, swap space acts as an overflow for RAM, storing inactive memory pages on disk to free up RAM for active processes. Traditional swap mechanisms are limited in flexibility and performance. The proposed "Swap Tables" aim to address these issues by allowing more efficient and dynamic management of swap space, potentially leading to better system responsiveness and resource utilization.

Linux Swap Table Code Shows The Potential For Huge Performance Gains

Comments Filter:
  • by yababom ( 6840236 ) on Friday May 16, 2025 @07:41PM (#65382141)

    For those unfortunate / foolish enough to be relying on swap...

    • by Sethra ( 55187 )

      This was going to be my response as well. I can't remember the last time I ran a linux instance with insufficient memory. But I can see use cases on SOC or IoT devices.

      • by dsgrntlxmply ( 610492 ) on Friday May 16, 2025 @08:13PM (#65382205)
        I have direct experience with this in consumer electronics. Working at minimum hardware cost alongside application flexibility and variation in externally supplied data, one can at times rapidly get into an acute memory shortage. A swap mechanism can give a few extra seconds to recognize the condition and to evict something to resolutely reclaim some space.
        • by ls671 ( 1122017 )

          Exactly this! All the people bragging about not using swap simply don't know what they are doing.

          And it's not only to attenuate memory leaks or to "give a few extra seconds to recognize the condition and to evict something to resolutely reclaim some space".

          I have a server with 128 GB ram and it will even swap stuff out when unused to make more room for buffer/cache in memory under heavy backup IO load to release it when the backup is over but it causes almost no swap activity at all. The kernel is smarter t

          • Exactly this! All the people bragging about not using swap simply don't know what they are doing.

            AHahaHahaHaHAHAHAHHAHAHA

            The kernel is smarter than you are and will swap out stuff for maximum performance of your system

            Maximum performance of my system does not include waiting for things which didn't need to be paged out to be paged back in.

            • by ls671 ( 1122017 )

              Reading comprehension problem maybe? Re-read what I wrote then tell me if you have the same amount of RAM as you have as storage space. If you don't then, we are back to square 1; "All the people bragging about not using swap simply don't know what they are doing".

              Sometimes it's better to swap some stuff out to make room for more buffer/cache performance wise and I haven't seen a system with as much RAM as storage space yet in my whole life. The kernel is unlikely to swap stuff you need anyway unless you ar

              • You also implicitly proclaim yourself as smarter than the kernel and Linus

                This is the single most false claim you are making anywhere in this thread. Though you are provably wrong about other aspects of this discussion, this is where you are most wrong. If the kernel didn't have some way to disable swap, then your claim might be accurate. However, it does have this function, so your claim is obviously false on its face. The existence of 0 swappiness and its ability to disable all swapping, and the ability of Linux to run without a mounted swap partition without complaint (or inde

                • by ls671 ( 1122017 )

                  Very simple: My server isn't short on RAM at all but it still performs better with swap although there is almost no swap activity (swap IO) so almost no SSD/nvme wear but it can use up to 40GB swap sometimes. Definitely reading comprehension problem I guess and you haven't told me at all if you were aware of buffer/cache importance for performance or even if you knew what buffer/cache were.

                  It doesn't matter how much RAM you have, your system will still perform better with swap because unused stuff will be s

                  • it still performs better with swap although there is almost no swap activity (swap IO) so almost no SSD/nvme wear but it can use up to 40GB swap sometimes

                    almost no swap activity
                    it can use up to 40GB swap sometimes

                    eyeroll

                    • by ls671 ( 1122017 )

                      Are you brainless? Seriously asking. Swap activity mean exchange between memory and swap media so yes you can have 40GB swap usage and no swap activity. Have you tried to learn about buffer cache yet?

                      Here is swap activity on my server for a period of 3 minutes:
                      iostat /dev/md127 180
                      All at zero 0

                      Here is the swap usage:
                      swapon -s
                      Used 20700284
                      So about 20GB are swapped out but 0 swap activity and that memory is used to gain performance by having more buffer cache.

                      NOTE: I had to remove the full output due

      • I've seen this on the fine print with the marketing of low-end Android tablets on Amazon and ebay.

        6GB of RAM - i.e. 4+2. but does anyone really want the wear and tear of writing to storage on media that is soldered in? Admittedly this is throwaway hardware that will end up in landfill/recycling within 4 years but still...

    • by gweihir ( 88907 )

      Yep, thought the same thing. Faking memory is not really for regular use ...

      • Fuck, I didn't think there were any idiots left with the "hrr hrr VIRTUAL mem0ry is for l0sers" mentality.
        • by gweihir ( 88907 )

          The problem is on your side. Because that is not what I wrote. Some clue required...

        • by ffkom ( 3519199 )
          The idiots are those who think a system can still perform well while swapping. In all the decades running different Unix flavors, I have never experienced a system that was not sluggish to unusable while it was swapping to persistent storage devices.
          • by gweihir ( 88907 )

            Indeed. Swap is a crutch. If you need it, sure, use it. But if you do not need it, do without.

    • Well it saved me a few times when dealing with a memory-leak/run away process.
      Second one was my programming mistake.

      It got me some extra time to kill the process that was consuming all the memory.
      • by ffkom ( 3519199 )

        Well it saved me a few times when dealing with a memory-leak/run away process. Second one was my programming mistake. It got me some extra time to kill the process that was consuming all the memory.

        If that is your use-case, this might be easier to achieve by using an allocator that deliberately slows down further allocations after a configured threshold is met. That way, only the software exceeding the threshold would be impacted, not every process requiring memory on the system.

  • by BrightCandle ( 636365 ) on Friday May 16, 2025 @08:02PM (#65382179)
    Linux uses swap even when RAM isn't full. VM_Swappiness means that it will push pages of programs memory use out if it thinks it can get more performance with the memory for file caching. More information on how this sort of works before this patch: https://www.howtogeek.com/4496... [howtogeek.com]
    • by Sethra ( 55187 ) on Friday May 16, 2025 @08:11PM (#65382199)

      It's almost never a good idea to leave this value at it's default. For example - the "tuned" profile "throughput-performance" will set this down to 5.

      • Re: (Score:2, Insightful)

        by drinkypoo ( 153816 )

        I always disable swap, unless I am on some pathetically limited hardware. I have 64GB RAM in my desktop and 8GB in my laptop, and no need for any swap on either. Swap just means the system thrashes more before the OOM killer kicks in, plus I don't need a bunch of extra writes on my SSDs.

        • by erice ( 13380 )

          I always disable swap, unless I am on some pathetically limited hardware. I have 64GB RAM in my desktop and 8GB in my laptop, and no need for any swap on either. Swap just means the system thrashes more before the OOM killer kicks in, plus I don't need a bunch of extra writes on my SSDs.

          My desktop also has 64GB of RAM but it does swap occasionally.

          • I run gentoo and some compiles can be really big
          • My backup regime compresses using lrzip, which will use tons of memory. It is quite a bit more efficient than less memory hungry compressors.
        • This is insane. Without swap, a single process can bring down the entire system if not properly ulimited.

          If I use up all of the RAM on the system, it's not necessarily me that dies from an sbrk() failure before the OOM killer kicks in.

          There's a reason machines have swap by default- it's because those that design them are smarter than you are.
          • by gweihir ( 88907 )

            You have no clue what you are talking about. With swap, a single process can bring down the entire system if not ulimited properly as well. It just takes a bit longer and may put serious load on your disk (not good) before things grind to a halt. Seriously, stop claiming nonsense.

          • by ffkom ( 3519199 )
            You appear to lack real-world experience with OOM situations. What you describe is entirely the opposite of what I experienced with OOMs on systems with and without swap space.
          • If I use up all of the RAM on the system, it's not necessarily me that dies from an sbrk() failure before the OOM killer kicks in.

            Greetings, program! I can assure you that no matter what, the OOM killer will not kill me. If you mean it won't necessarily kill the problem process, guess what? That was always true, and it is true whether you have swap enabled or not. The only difference is that in the case of a runaway process on a system that does have swap, the system thrashes longer before something gets killed.

            There's a reason machines have swap by default- it's because those that design them are smarter than you are.

            Machines do not have swap by default. Machines do not even have operating systems by default. I've been running Linux without

        • I used a 4GB 2008 laptop until half a year ago when hardware issues forced me to replace it (this one a 2020 Dell with an upgrade to 32GB for the heck of it).

          Swap was essential for daily use because the browser already used up loads of RAM (not even using Firefox or Chromium) and I activated zswap some years ago eventually when I read about it.
          It did the trick of improving overall peformance and seemed to save on disk read and writes as well from observation, and I still have zswap enabled on this laptop ju

        • I always disable swap, unless I am on some pathetically limited hardware. I have 64GB RAM in my desktop and 8GB in my laptop, and no need for any swap on either. Swap just means the system thrashes more before the OOM killer kicks in, plus I don't need a bunch of extra writes on my SSDs.

          I always have craploads of swap precisely because it makes thrashing much much less worse when I do exceed the RAM. I do have enough RAM, but I sometimes screw up. If you're dealing with multi-10 GB datasets it doesn't take

        • by gweihir ( 88907 )

          Same here. Linux is, unlike traditional UNIX, designed to run be this way as well. This becomes important in diskless setups (small embedded uses, for example) and convenient in desktop or server use with plenty of memory. As a bonus, you do not have to worry about secrets ending up in swap if you run swapless and things become a bit simpler, which is always a plus.

          • I saw elsewhere that you said Slowlaris still has problems with this? Ugh. I remember back in the SunOS4 days having to allocate (memory + desired swap) MB of swap partition :) My first personally-owned actual-Unix was SunOS 4.1.1 on 3/260... you had to link the DNS resolving library into libc if you wanted more than NIS, too.

            • by gweihir ( 88907 )

              As far as I remember, Slowlaris places some device-special files on swap or something and that stops working with no swap. I never personally ran any UNIX-like thing on my systems besides Linux and, rarely, freeBSD. My first one was Linux Suse April 94 on a machine with 4MB. Did not do too badly.

              • I used to be an OS hobbyist, so I've owned three Suns, a couple of SGIs, Apollo Domain workstations, IBM RT PCs, an Alphastation, a BeBox, and probably some other weird stuff I'm forgetting.

                Now I just want stuff to work.

                My first Linux box had 8MB, that was fairly generous at the time.

        • by allo ( 1728082 )

          It is never bad to have swap space.

          https://chrisdown.name/2018/01... [chrisdown.name]

          • by ffkom ( 3519199 )

            It is never bad to have swap space.

            https://chrisdown.name/2018/01... [chrisdown.name]

            There is some fundamental hole in the line of arguments there: If having swap-space was such a blessing, then the kernel could just use N GB out of the N+M GB of physical RAM that the system has as "swap space", without any persistent storage to back it. But apparently, doing this is not considered worthwhile.

            The linked article also writes:

            Without swap: The OOM killer is triggered more quickly as anonymous pages are locked into memory and cannot be reclaimed.

            Yes, and that is a good thing! If the system is OOM, I want the OOM killer to act right away. Anything else is not welcome. I would even welcome if executable code was n

            • by allo ( 1728082 )

              "If having swap-space was such a blessing, then the kernel could just use N GB out of the N+M GB of physical RAM that the system has as "swap space", without any persistent storage to back it."

              You're missing the point. If you reserve M GB for RAM-Swap to move the pages there, then you can also just use N+M GB and keep the pages in RAM. Swap is the option to move the pages to disk to have more RAM for optional pages.

              In the end the article describes it a lot more details, but the bottom line is: With swap app

              • by ffkom ( 3519199 )

                Swap is the option to move the pages to disk to have more RAM for optional pages.

                I have no use for "optional pages". It's like arguing for or against measuring performance in terms of worst-case versus average-case. I for one am interested to keep the worst case cost of memory accesses to that of physical RAM accesses. Even if under specific circumstances the kernel could find pages that are rarely used, I do not want them to be moved to persistent storage. To me the only viable solutions are "install more RAM or use less RAM", not "make RAM accesses a game of chance, could be fast, cou

      • It's almost never a good idea to leave this value at it's default. For example - the "tuned" profile "throughput-performance" will set this down to 5.

        Yah that still swaps. Linux will fill all physical memory with filesystem cache first. Then does one of two things for each cache miss. At a swappiness of 5, for every 100 pages read, 97 pages of old FS cache will be replaced and 3 of some process's idle memory will get swapped out. All you're doing is slowing it down, and over time you'll always have a bunch of process memory swapped out. The actual rate it happens at depends on your workload, how many cache misses you get over time. A daily filesystem bac

        • Yah that still swaps.

          This is the very worst thing about the swappiness control. There is no setting for "swap only when it's actually necessary". You can either have "don't swap" or "only swap a little bit even when you don't need to" and that's inadequate. Where's "swap only when memory is actually getting tight, and it might actually become useful"? There's no such thing, instead it creates more write iops even when there's no need for them just in case there might be a need someday, at the cost of SSD lifespan.

          Swap isn't bad.. it's a form of tiered storage really.

          Swap was alway

      • The fact that it is not set to zero should tell you something important.
      • by PPH ( 736903 )

        For example - the "tuned" profile "throughput-performance" will set this down to 5.

        My rule of thumb: Turn everything up to eleven.

    • by tlhIngan ( 30335 )

      Yes, this can increase performance. Basically if the system is idle enough, Linux can write changed pages to disk ahead of time so if it needs to swap out the pages, they've already been committed to disk and can immediately be reallocated on demand.

      Basically instead of writing data to disk when you need to, you write it ahead of time so if it's needed, it's already done. It requires a balance - you don't want to be too aggressive and writing data constantly to disk because you can use up valuable disk I/O

  • by rossdee ( 243626 )

    Why do we need virtual memory these days?

    • Re:Why (Score:5, Informative)

      by BrightCandle ( 636365 ) on Friday May 16, 2025 @08:17PM (#65382219)
      Because it improves performance by saving drive reads and writes. For example we have a program in memory and its doing nothing sat idle consuming half of all our memory. But we are using another program which is reading lots of files in and writing out updates. The second program if given more RAM for caching the file contents its reading would run considerably faster, the NVMe SSD might read at a few 1GB/s but RAM is going to be 100x faster. If the OS pushes the unused programs memory out onto the swap we can that memory for file cache and get better performance for the current program doing work. We can also cache writes into files and only push completed large groups of blocks back to the drive allowing bigger sequential writes which results in less IO overhead and sequential access. If you have ever run CrystalDisk you will know is substantially faster to do this than apparently random writing or individual blocks of 4k.
      • If your memory is so tight that you need all this shuffling, maybe you should invest another $25 for more RAM!

        • Your statement isn't wrong, but only really valid for desktops. Sure, top-class laptops have replaceable RAM, but even on mid-class laptops RAM is being soldered and can't be upgraded anymore. Soldered RAM is pretty much the only option you'll get in low-class laptops. Margins have been very thin for quite a while now in "laptop land". And soldered RAM is cheaper than installing connectors to have upgradeable RAM. As a manufacturer, that choice is easily made.

          Pop!_OS 20.04 ran marvelously well in my Lenovo

          • by gweihir ( 88907 )

            That is some advanced nonsense. Servers run just as well as desktops swapless. The only thing swap does on Linux is that running out of memory comes a bit later.

          • You're right about laptops. They are hard to upgrade, and this is a big problem for me as a developer.

        • by allo ( 1728082 )

          Invest another $25 in more RAM *and* use Swap and you have even more memory available.

      • Because it improves performance by saving drive reads and writes.

        Swap functioning increases the number of drive reads and writes.

        If the OS pushes the unused programs memory out onto the swap

        Then those programs allocated memory before they needed it, which we know because they weren't using it. Those programs are bad, and/or they were tuned incorrectly.

        If you have ever run CrystalDisk you will know is substantially faster to do this than apparently random writing or individual blocks of 4k.

        When I built my system, I put enough RAM in it to both run programs and have disk cache. When we were in the RAM crisis swap made a lot of sense, not least because systems commonly had memory measured in single digits of megabytes. On portable devices, zswap perhaps makes sense, beca

    • by evanh ( 627108 )

      Virtual Memory does not require swapping ability to still be VM. Swapping is an optional add-on feature.

      • by evanh ( 627108 )

        BTW: Swap space is still a physical memory type, just like DRAM, but much slower access.

    • Re:Why (Score:5, Interesting)

      by darkain ( 749283 ) on Friday May 16, 2025 @08:42PM (#65382259) Homepage

      I understand the usage of the term "virtual memory" here, as its been incorrectly correlated to swapping memory to disk since its inception really... but i do hope at some point the realization in the broader community that "virtual memory" literally means all memory on a modern operating system's user space. this is how we can get multiple instances of the same application running at once, as they're using the same virtual memory addresses that are then translated over to physical addresses by the memory controller, which is controlled by the operating system. This memory is handled in chunks called "pages", and those individual pages can be moved around to different physical memory addresses without changing their virtual memory addresses within the user space application. these physical addresses can also land on disk, which is where the "swap" comes from, also sometimes called the "pagefile" - but the term "virtual memory" has been used so long by so many people to reference just swap/page file space on disk, at this point its real meaning sadly has been lost and i personally think that sucks.

      • We use the same words or phrases for different things. Sometimes this leads to confusion.

        • by evanh ( 627108 )

          This is not one of those cases. Virtual memory is not swap. Anyone that conflates the two has just got it wrong.

          • You might find it helpful to read the summary.

            Context: In Linux, swap space acts as an overflow for RAM, storing inactive memory pages on disk to free up RAM for active processes

            This is indeed talking about swapping memory pages to disk.

            • by evanh ( 627108 )

              Yes, the question asked "Why do we need virtual memory these days?" looks to be conflating VM with swap. Hence the attempts to clarify.

            • by allo ( 1728082 )

              overflow is wrong, because it implies that swap is only used when the memory is full.

      • Re:Why (Score:4, Informative)

        by Tony Isaac ( 1301187 ) on Saturday May 17, 2025 @12:07AM (#65382555) Homepage

        According to the summary:

        Context: In Linux, swap space acts as an overflow for RAM, storing inactive memory pages on disk to free up RAM for active processes

        This is clearly referring to swapping memory to disk, not your proposed concept of "all memory on a modern OS user space."

        • by evanh ( 627108 )

          The question asked "Why do we need virtual memory these days?" looks to be conflating VM with swap. Hence the attempts to clarify.

      • Not just userspace. Kernel doesn't operate on unmapped memory, either.
        Also, the translation happens on the CPU well before the memory controller (or even the root complex).
        • by cstacy ( 534252 )

          Not just userspace. Kernel doesn't operate on unmapped memory, either.

          Also, the translation happens on the CPU well before the memory controller (or even the root complex).

          You are correct in correcting someone there, but I'll just mention that originally, in the old days, CPUs did not have virtual memory and it was done by a separate memory controller circuit. They sometimes called it a Memory Management Unit (MMU). This was the case even with advanced microprocessor chips, not just the really ancient iron.

      • by cstacy ( 534252 )

        Mapping tricks notwithstanding, VM was more interesting in the old days when you had much more RAM than address space. (And we had demand paging / swapping, too. Just less likely to get there.)

      • No, that is not what "virtual" means.

        We have 3 types of hardware:
        a) physical hardware, hardware that really exists and is if a certain type. As in drives: the type is drives. As in keyboards, the type is a keyboard
        b) logical devices, this are devices that do not exist, but are backed by a physical device of the exact same type. So you have a hardware clock ... and dozens of logical timers ... does not really matter how the timers are set up: the timers are backed by a physical clock, which does: count time
        c

    • Well, for one specific case; I needed to start up four VMs in my home lab for a test where each one needed 16GB of RAM, my system had 64GB total, and I didn't feel like going and buying several hundred dollars of RAM, throwing away the 64GB I have already in order to do it.

      • by Anonymous Coward

        Dunno, man.

        For a grand, get yourself a Turing Pi cluster with up to 32GB on each compute module. :)

        https://turingpi.com/product/t... [turingpi.com]

      • That is a different use case where you wanna use KMS memory merging itâ(TM)s built into the kernel.

      • by cstacy ( 534252 )

        Well, for one specific case; I needed to start up four VMs in my home lab for a test where each one needed 16GB of RAM, my system had 64GB total, and I didn't feel like going and buying several hundred dollars of RAM, throwing away the 64GB I have already in order to do it.

        It's too bad the virtual machine instances can't share pure pages with each other.

    • I suspect, in part, the same reasons that we historically did. You are much less likely to be on a 32 bit system and it's a lot easier to afford a lot of RAM in absolute terms; but it remains pretty expensive to have more RAM than you'll ever need, mass storage remains considerably cheaper than RAM, and it's typically still preferable to have things slow down when you run out of RAM rather than having the OOM killer sweep through.

      There's also the sheer convenience(and potentially better outcomes, dependi
    • by gweihir ( 88907 )

      You do not. I have it turned off on most of my Linux boxes. But it can still have benefits.

      • That is a terrible idea, for performance and for stability.

        It's alright as long as you never have any kind of memory pressure, but then that just means you're really wasting a good portion of your RAM.
        • by gweihir ( 88907 )

          Nope. That is actually an expected use-case today. For a _classical_ UNIX it would be a bad idea, but Linux is more advanced. For example, systems like Solaris may even kernel-panic without swap. Linux does not. Also, in the classical situation, not having enough memory was normal. The modern situation is that memory is plenty and the amount you save is rather small in comparison. Of course, the used kernel must have been designed with swapless operation in mind. Linux is and you seem to be stuck a bit in t

      • I still have swap enabled on my current 32GB laptop even thought it's not used after an uptime of a month at the moment.
        Zswap on top of that enables more (efficient) buffer space as it compresses old pages eligible for swap.

        Then there's also the need for swap to enable hibernation to disk.

        • by gweihir ( 88907 )

          Hibernation is a really bad idea, for a number or reasons. One is that it is hard to secure it.

    • For address-space isolation. Swap is just a way of overloading RAM, but virtual memory has other benefits, especially the ability to run programs in entirely separate address spaces. It provides a useful mechanism for hibernation, as well. A sexier aspect is building an address space with shared read-only components and read-write areas for your applications - I'm not sure if Linux can do this, but IBM's VM operating system did this back in the 1980s.

      • by gweihir ( 88907 )

        Indeed. Virtual memory is not even required for swap or the other way round. It is just convenient to do both at teh same time as the needed hardware in the MMU has overlaps. But running swapless does in no way mean running without virtual memory.

    • by allo ( 1728082 )

      Because free RAM is good RAM, because it can be used for caches.

      Let's say you have a process that does nothing but sleeping. Maybe it is waiting on user input, waiting on network I/O or generally unused. Or maybe it is used, but has a memleak that is not enough to bring the system down, but still blocks quite a bit of memory.
      If you swap out its rarely accessed memory, you can use that RAM as read-cache. Having that RAM as read-cache means you don't need to read less from the disk, resulting in less I/O.

  • I wonder what performance impact it'll have on zram-generator (swap in compressed RAM). https://github.com/systemd/zra... [github.com]
    Anyone interested in that may want to use Fedora's default settings. https://aur.archlinux.org/pack... [archlinux.org]

  • #!/etc/sysctl.conf
    vm.swappiness = 0

    I really don't know what else to tell you. Just add more RAM.

IF I HAD A MINE SHAFT, I don't think I would just abandon it. There's got to be a better way. -- Jack Handley, The New Mexican, 1988.

Working...