To be clear, I don’t blame the poster of this comment at all for the content of their post – this is accepted as “common knowledge” by a lot of Linux sysadmins and is probably one of the most likely things that you will hear from one if you ask them to talk about swap. It is unfortunately also, however, a misunderstanding of the purpose and use of swap, especially on modern systems.

  • 4wd@programming.dev
    link
    fedilink
    arrow-up
    20
    arrow-down
    2
    ·
    3 months ago

    I’ve never understood why GNU/Linux actually needs swap. Okay, I created a 4G partition for it, having 32G of RAM. I never used all that RAM, but even so, stuff regularly ends up in swap. Why does the OS waste write cycles on my SSD if it doesn’t have to?

    However, if I artificially fill up all 32G of RAM, the system almost completely freezes faster than switching to using swap as a “lifeline”. And it only comes back to life when OOM Killer finally remembers its existence and kills some f__ing important process.

  • Balinares@pawb.social
    link
    fedilink
    English
    arrow-up
    18
    ·
    3 months ago

    Great article, thanks for posting! Worth noting that swap is also used for tmpfs partitions. Meaning that if you don’t have any swap, temporary files in /tmp will use your actual physical RAM. That’s probably not what you want.

  • recursive_recursion [they/them]@programming.dev
    link
    fedilink
    English
    arrow-up
    12
    arrow-down
    2
    ·
    edit-2
    3 months ago

    definitely agreed on against using swap as memory as much as you can especially since it can needlessly wear out your ssd (m.2 on most if not all on modern systems)

    allocating swap is still necessary as it provides features such as:

    • mitigating OOM scenarios (but doesn’t prevent them completely)
    • enabling system hibernation(suspend-to-disk)

    On my journey to learning Arch Linux I’ve personally tried:

    • 0 swap: which was pretty awful with constant unexpected system freezes/crashes
    • swap file: finicky but doable
    • swap partition: typical default/ol’reliable

    the last two doesn’t guarantee 0 problems as user errors such as allocating too much system memory to VMs can cause issues for the host system, but it does tend to mitigate the majority of issues that prevent the perception of system stability in comparison to Windows or Macs

    Resources:

      • recursive_recursion [they/them]@programming.dev
        link
        fedilink
        English
        arrow-up
        2
        arrow-down
        3
        ·
        edit-2
        3 months ago

        after a quick preliminary search,
        a couple of things I’ve found out:

        this might not necessarily be adopted by the mainstream desktop users, due to the nature of zram compressing data in addition to disallowing hibernation

        Hibernating to swap on zram is not supported, even when zram is configured with a backing device on permanent storage. logind will protect against trying to hibernate to a swap space on zram.

        • data compression by nature incurs dataloss so there are both pro & con tradeoffs to which should be communicated to the user otherwise they might receive an unnecessary worse user experience:
          • “why is my computer so buggy?”
            • (the system is working as it’s coded but not working in they way the user expects it to)
              • this would be unfortunate as they might leave back to Windows or MacOS
          • I could be completely wrong about this, if so please comment or disregard this section entirely

        to me atm; zram seems great for server based systems rather than for desktop users’ systems


        one other method for zram mainstream adoption is to encourage an eccentric system that I’m currently using, which is to have the host system only contain minimal packages to run Virtual Machines:

        • zram compression might be fine as the main services/programs are run within their relevant and/or separated VM containers
          • this simultaneously achieves:
            • Wikipedia - Separation of Concerns
            • enhanced security as the host is further firewalled/bubblewrapped from the guest like Flatpaking/containerization (and malware is less likely to activate due to malicious users being unhelpful in letting their programs run in a VM 😢🤗)
        • hibernation can still be achived by save-stating VMs (VM restoration at will basically achieves hibernation so it doesn’t really matter if the host is shutdown so long as all VMs are saved by the user before host shutdown)

        sorry for the long comment!
        stuff like this interests me the most so I’ve spent a lot of time in learning and tinkering around🤗

  • lurch (he/him)@sh.itjust.works
    link
    fedilink
    arrow-up
    14
    arrow-down
    4
    ·
    edit-2
    3 months ago

    I’m running Linux without swap for 20 years on my workstations and gaming PCs now. If you don’t hybernate and have enough RAM swap is useless.

    My memory doesn’t need to be managed. I have 20GB in my current setup and it was never full. If anything gets swapped in this situation it means it needlessly slows me down.

    I even mount tmpfs ramdisks for my shader cache dirs, because they get recreated every time anyways and why would i want anything temporary on disk, if i have 20 GB of RAM.

  • zero_spelled_with_an_ecks@programming.dev
    link
    fedilink
    arrow-up
    6
    arrow-down
    1
    ·
    3 months ago

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

    This is why I prefer no swap on servers. I want oom to kill things quickly, otherwise everything slows to a crawl including ssh and terminal sessions that I’d use to troubleshoot and kill things myself anyway. I have redundant servers so a down system is much less disruptive than a terminally slow system.

  • nixfreak@sopuli.xyz
    link
    fedilink
    arrow-up
    5
    ·
    3 months ago

    This is a great article , and actually rather use swap than no swap. My systems run so much better with swap.

  • boredsquirrel@slrpnk.net
    link
    fedilink
    arrow-up
    2
    arrow-down
    1
    ·
    3 months ago

    Swap is only needed for emergencies, if you have a bad oom, and if you have low RAM.

    If you have like 1-4GB of RAM, ZRAM may be better than swap.