Ask
25
@memory_meltem ·

Does a machine with plenty of RAM still need swap?

I am setting up a machine with a comfortable amount of memory and the installer wants to allocate swap space. The advice I find ranges from making it twice the RAM, through a fixed small amount, to not bothering at all because there is enough memory.

Those cannot all be right, and they seem to come from different eras.

What is swap actually for on a modern system, and how should I size it?

4 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @memory_meltem · last wk.

    The advice comes from different eras because what swap is for has changed, and most of the rules of thumb are answering the old question.

    The old purpose was to run programs that did not fit in memory. With plenty of RAM that rarely happens, which is where "you do not need it" comes from.

    The modern purpose is different and still applies: swap lets the kernel move rarely-used pages out so that memory can be used for something better. A long-running system accumulates pages that have not been touched in days — startup code, an idle daemon's initialisation. With swap, those go out and the memory becomes cache. Without it, they sit there occupying RAM that would be doing more good as file cache.

    So a system with no swap and plenty of memory is not faster. It is slightly worse at using what it has, and it has no margin when something unexpected allocates.

    30
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @disk_dilek · last wk.

    Worth knowing about compressed swap in RAM, since it changes the calculation on small machines and is now a default on several distributions.

    Instead of a partition on disk, a block of memory is used as compressed swap. Pages are compressed rather than written out, which is far faster than a disk and gives you an effective memory increase of roughly two or three times for compressible pages.

    On a small single-board machine or a laptop with limited memory, this is the single most effective change available, and it is often just installing a package. It combines fine with a small disk swap as a second tier for genuinely cold pages.

    It is also much kinder to a solid-state disk than heavy conventional swapping, which is the other reason it has become the default.

    22
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @memory_meltem · 7d ago

    How to size it, in practice:

    • A few gigabytes is enough on a machine with plenty of memory. The old doubling rule was about fitting the whole of memory for hibernation and for running oversized programs, and neither drives the number now.
    • If you hibernate, you need swap at least as large as RAM, because that is where the image goes. This is the one case the large numbers still apply to.
    • Zero swap is a defensible choice on a server with a well-understood workload and monitoring, and it makes the failure mode abrupt: instead of degrading, the out-of-memory handler kills something. Some people prefer that, deliberately.

    The other lever worth knowing is swappiness, which controls how eagerly the kernel swaps. Lowering it does not disable swap; it makes the kernel prefer dropping cache first. On a desktop that usually feels better; on a server it depends on whether cache or resident memory is doing more for you.

    27
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @fs_ferhat · 6d ago

    One practical note: a swap file is as good as a swap partition on any modern kernel, and it is far easier to resize.

    So the argument about how much to allocate at install time is largely obsolete — allocate a modest partition or none, and add a file later if monitoring shows you want more. Deciding a permanent partition size at install, for a machine whose workload you have not run yet, is the part of the old advice that has aged worst.

    15
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report