Linux nerds, here is a question,

I have a machine with 32 GB Ram, currently it is using 12.5 G and at the same time I see swap is used 1.55G/2.00G

why swap is used if i have enough Ram ?

  • 12
    Because it's stuff that isn't used anyway. You can decrease the inclination to swap.

    cat /proc/sys/vm/swappiness (that's probably 60)

    Edit /etc/sysctl.conf as root. Add/modify the following line:


    (Or maybe 20, something like that.)
  • 5
    ^^ what he said. Or set it to 0. Or even disable swap if you don't want apps to use it
  • 5
    With 32 GB of ram, you should not even need swap
  • 0
    Swap is enabled by default for linux.
    even if you disable it completly, and remove the swap partition setup in fstab, the kernel will still try to use it anyways.
    Kubernetes for example, does *not* like swap.
  • 9

    > Swap is enabled by default for linux.

    Is it though?

    If you choose a fully automated install - then perhaps. But for all the setups I've installed manually, I had to explicitly specify a swap partition to use if I wanted one.
  • 3
    @netikras No idea 🤷‍♂️.
    Too many linux distros, and too many install options.....
    The best imo, is to use a custom VM images for k8s clusters.
  • 1

    Yep, this.
  • 3

    Id like to interject for a moment...
  • 3
    Because caching regularly used disk content is better use of the memory than keeping never used pages there. The ratio can be configured by vm.swappiness.
  • 4
    Didn't we had that discussion not so lang ago....

    Swap isn't an RAM extension.
  • 3
    because paging out memory that hasn't been accessed for (relative) eons frees the ram to be used as cache, which improves performance.
  • 2
    *cries in page fault*
  • 2
    @IntrusionCM smth is going on. Seems like a surge of questions about swap lately. It wasn't there until like a month ago.

    Did some country switch to all-linux?
  • 0
    > isn't a ram extension

    to some extent it kind of is :) and still you haven't convinced me that swap's purpose is anything but *extending ram's* 'cold storage' for unused pages :) bcz the articles you referred to were explaining exactly that, i.e. how kernel does that with swap.
  • 1
    @netikras everyone has switched to Linux recently, including me ;)
  • 1

    It's an extension to the memory available for the kernel - kernel memory, not RAM.

    Nitpick, but it makes a difference.

    And the kernel can not store everything in a swap - there are rules.

    Nitpicky, again, but makes a difference.

    An extension would mean to me that it can be used in the same way as RAM - which isn't possible.

    It is not an substitute either, as the kernel requires RAM to boot.

    Which brings me back to the beginning - it's a form of extension to the memory available for the kernel, not RAM.
  • 1
    (If I'm currently sounding bitch, I'm having migraine like headaches at the moment. Pain makes me very ... Bitchy)
  • 1
    @IntrusionCM You bitch!!! :)

    naah, you sound alright.

    Just to be clear, only kernel can make memory allocations. An app does not know nor does it care what hides behind the term "memory", as long as there's some for it to allocate. Where do these pages go afterwards - ... meeh.. Not app's problems. So when you say it's a memory extension for kernel - that's both wrong and right, depending on the PoV :)

    I would completely agree with you IF having more swap did not boost the available address space, i.e. apps could not overcommit RAM on swap's account. And kernel only used swap for its own business, having no effect on the applications' behaviour. But now.. a single app cannot overcommit RAM, but all the processes running on the system in total - can, when swap is enabled.
  • 1
    @netikras Hm... Yes.

    Though the application CAN tell the kernel what needs to be done.

    Look at e.g. madvise and mlock.

    That's why I think more of kernel memory management then RAM.

    mmap with anon isn't swappable either, if my mushroom of brain isn't wrong.

    So as you said - you usually don't use RAM, you use the kernels memory management system and all the shenanigans it offers.
  • 1
    could it be we disagree bcz we're looking at it from different perspectives?

    from kernel's PoV - yes, it's not a RAM's extention, but rather a mechanism helping to reduce pressure on RAM. Pages in swap cannot be used directly - they still have to be moved back to ram before accessing them [though idk if that's true for non-data-read/write access].

    From app's/user's PoV - I don't see how swap is different from RAM, as it is the same big memory pool. It asks the kernel to allocate a memory chunk - and the kernel allocates, regardless of whether ram is full or not [assuming there's enough space in swap]

    does that sound right?

    Gosh, I love how well brain works right after waking up :)
  • 1
    @netikras might be.

    Highly likely I would say.

    Though - as mentioned - the app can absolutely say what it wants.

    madvise / mmap / mlock as I said before. ;)

    I'd guess that's where my point stems from. RAM is kinda useless without the kernel managing it - as u said.

    But the kernels memory management is vastly more complex than "that's RAM, that's swap". I'd guess that's why I'm so nitpicky.

    Kernel memory management is a fickle thing which can "entertain" one for days without understanding it at all.

    Guess that's why this is one of the things I'd like to torture myself regularly with.

    But in database and any complex server app, there is a certain need in my opinion to at least grasp the basics, otherwise you'll be royally fucked.

    I'd guess that's the other reason I love and hate swap discussions.

    It's an entirely different thing comparing desktop vs server / complex application use.

    Swap and constant memory pressure is a thing that makes no fun in desktop apps, many user suffer from it cause they absolutely have no frigging clue how to use their resources probably.

    Pisses me off regularly - cause it is just dumb.

    But in server applications? That's not dumb. That's suicide.

    Database running on constant memory pressure and wasting CPU cycles and burning IO coz one wasn't able to do simple mathematics? That's... Imho an reason for serious talk if the admin is fit for the job. After all, the database is suffering major performance penalties and thus every application based on the database is suffering. That's clusterfuck exponential.
Add Comment