Live data from Hacker News

Linux Performance: Why You Should Almost Always Add Swap Space

haydenjames.io

21–30 of 120 posts

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#21
If you must use swap (you probably don't need to), then at least set zswap.enabled=1 in the kernel boot options, typically in grub. This will enable lzo compression of swap in memory so there is less writing to disk. Some newer kernels use lz4.

Either way, what most folks are actually missing is the correct kernel settings for the amount of memory they have. Sadly, the kernel does not dynamically adjust these based on your total amount of memory. Below is based loosely on Redhat suggestions. Please note, that if you fall below the min free, the kernel will decide what to do next, based on your oom and panic settings. The settings below will have the kernel free cache and other memory earlier so that you do not hit those stalling conditions and can even prevent some OOM race conditions. Some might suggest tuned, but use tuned with caution or at least read up on everything it does.

    MEM=`grep ^MemTotal /proc/meminfo | awk {'print $2'}`
    if   [ ${MEM} -gt 1129241478 ] ; then
        sysctl -q -w vm.min_free_kbytes=16384000
    elif [ ${MEM} -gt 564620739 ] ; then
        sysctl -q -w vm.min_free_kbytes=8192000
    elif [ ${MEM} -gt 352887962 ] ; then
        sysctl -q -w vm.min_free_kbytes=4096000
    elif [ ${MEM} -gt 176443981 ] ; then
        sysctl -q -w vm.min_free_kbytes=1024000
    elif [ ${MEM} -gt 88221990 ] ; then
        sysctl -q -w vm.min_free_kbytes=524288
    else
        sysctl -q -w vm.min_free_kbytes=262144
    fi
If you have small VM's, then perhaps set the default above to something a little smaller. You can of course free up about 128MB on default installations by removing "crashkernel" from your grub config and rebooting.

Then do this regardless, because overcommit set to 0 does not mean off, so we set the ration to 0 as well. Overcommit is good for developers testing code and finding the correct ways to manage memory in their applications during development.

    sysctl -q -w vm.overcommit_ratio=0
And of course, cache pressure plays into early evacuation of the right cache based on your usage:

    ## default is 100 (optimal for file servers).  4000+ for in memory databases.
    ## 10000 means always prefer page cache.
        sysctl -q -w vm.vfs_cache_pressure=1000
And if you have people oversubscribing a lot: (adjust based on your memory capacity)

    sysctl -q -w vm.admin_reserve_kbytes=131072
    sysctl -q -w vm.user_reserve_kbytes=262144
Please do read up on all of these before testing on your test machines. [1]

[1] https://www.kernel.org/doc/Documentation/sysctl/

Then finally, make sure you have Transparent Huge Pages disabled unless you know for sure you need it. THP can leak a lot of memory and it is nearly impossible to see without extensive kernel debugging.

In grub, set this and reboot:

    transparent_hugepage=madvise
Or to manually disable THP during run-time,

    echo -n "madvise" > /sys/kernel/mm/transparent_hugepage/enabled
    echo -n "never" > /sys/kernel/mm/transparent_hugepage/defrag
Then restart your applications. THP defrag can also cause stalling and lag spikes, especially in large memory java deployments (it will look like FGC's) and in MongoDB, Cassandra, others.

If you did this manually, stop your apps, flush cache, compact memory, then start your apps.

    sync;sync;sync
    echo 3 > /proc/sys/vm/drop_caches
    echo 1 > /proc/sys/vm/compact_memory
Some will say 3 sync's is not required. This is mostly true, but some old raid controllers treat this differently.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#22
In my experience, I think engineering swap usage is more complex than this article leads on. While adding more swap may make sense for many systems, I've personally worked on several systems where disabling swap made a huge difference. It's just one of those things where there isn't a hard rule.

One of the issues I've encountered more than once with Java software, is what appears to be longer than expected pauses in the GC, that I've linked to swap usage. The theory is, if you have a reference that lives long enough to get promoted to an old generation, but then is released, the memory in question won't be used for quite some time. This can lead the kernel to detect the memory pages not being used, swap them to disk, but then when it's time to do GC, the process is just frozen while the GC is running and get's backed up. Depending on the workload this might be fine, however, on both the platforms I encountered this issue on, were real time applications which were engineered to have available memory for the application and did (and also had no benefit from using the free'd memory for IO cache). So in this case, disabling swap had a real and measurable performance boost for us, getting rid of some of the variability in our GC runtime and pauses.

Under low memory conditions I think is also subjective. So if I'm a distributed / highly available system that's doing message routing in a telco environment, I would rather have the OOM killer kill the process, and failover to a backup, then for the system to be up but unresponsive because it's spending a majority of it's time swapping too/from disk. Again, this totally depends on the platform.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#23
This is dangerous advice. At least on Ubuntu 16.04 with stock kernel running due to memory pressure into swap kills the systems. Basically everything times out and you can only hard-reboot. Without swap you have an OOM at least something you can recover from. If you need swap just add a little like 1-4gb - because if you have spinning disks 64gb swap won't help you at all because every request to swap is magnitudes more slower. Might be different with NVMe but it's still noticable.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#24
post #15

I wondered this for a while, unfortunately a major question I have remains unanswered in the text: What about SSDs and SSD-only systems? (Like... any average laptop.) Should I be worried that putting a swap on an SSD will cause it to wear out fast due to many write cycles?

Same here. How does file swap (cf. https://wiki.archlinux.org/index.php/swap#Swap_file_creation) could help out to let an underlying file system distribute the usage of the ssd? Combined with recreating a swap file on every boot (say on a frequently booting desktop machine), this could make a difference.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#25
post #23

This is dangerous advice. At least on Ubuntu 16.04 with stock kernel running due to memory pressure into swap kills the systems. Basically everything times out and you can only hard-reboot. Without swap you have an OOM at least something you can recover from. If you need swap just add a little like 1-4gb - because if you have spinning disks 64gb swap won't help you at all because every request to swap is magnitudes m…

indeed on ubuntu 16.04 64bit, when my chrome has 20+ tabs, some new tab could trigger a memory thrashing, that uses up all the memory and eats up a few GB disk, cpu load jumps up to 10x, the computer basically becomes unusable.

i would rather a OOM kill the chrome process in this case, it wastes less time on me, a re-open of chrome will keep all the histories anyways.

also on embedded system where the RAM is fairly limited, and no hard drive attached, and its storage is measured by MB instead of GB, you simply can not have the swap partition.

I want to run the system without swap actually.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#26
I'm not going to disagree with the article, because I haven't done any testing myself that would suggest that the point made are wrong. I do however find it amusing to login to a system with 32 or 64GB of memory with a 2GB swap. It seems humorous that those 2GB should somehow do anything if you already used 64GB of RAM.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#27
Cargo cult system administration.

No, you should not 'almost always add swap space'. What you should do instead is tune your system for its intended use.

If you need swap as an 'early warning system' that you're about to run out of memory you're already doing it wrong and the OOM killer is a piece of code that has default settings that can be tuned, ditto for the virtual memory manager in the kernel.

http://www.oracle.com/technetwork/articles/servers-storage-d...

https://access.redhat.com/documentation/en-us/red_hat_enterp...

Select appropriate equivalent for your kernel where applicable, and don't forget about the per-process limits in ulimit.

This sort of 'almost useless' advice is pretty annoying, it shows that the writer has an interest in the matter but didn't bother to dig in deep enough to make the advice truly useful.

Also, the comment on that article that implies that swap will allow you to update a running process because it is backing the image is wrong, the backing is simply done through the filesystem and swap space has nothing to do with it. The practical upshot of which is that you're not going to be able to reclaim the space held by the binary if you should remove it because the 'file is busy' until the last instance of that process exits. In the meantime, if you really want to run the newer version under the old name or path then you're free to unlink it or 'mv' it to a different location and to start the new binary from the default location. On some Unixes you can even overwrite the binary using an mv command making it look like the old one has disappeared but again, under the hood it will still be held open, you can check this using lsof or whatever is your local equivalent to see that the file is indeed still present. In a pinch (and you really really have messed up if you ever need this trick) you can re-link the running image by hard linking to the inode.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#28
post #17

Earlier quoted context omitted.

This sounds as if it is a serious issue and should be fixed. Is there a bug report about it?

This is due to memory overcommit, it's a performance feature. You can turn it off.

Interesting, I sort of remember an interaction with NUMA as well. I don't believe this is a bug, just a miss-understanding of the way the underlying system works. From what I remember when swapiness is 0, there are lots of reports of processes that use more than a single NUMA node worth of memory getting killed by the OOM killer, even with plenty of free RAM available. I unfortunately don't remember the details, but this prevented the memory from being able to be allocated to the additional node.

I've heard of this mostly with mysql, where it's common to have a big server with lots of RAM, but a single large process that uses most of the system RAM. The way we got around this, was by setting the process to allocate interleaved among the NUMA nodes.

I'll have to dig into the overcommit.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#29
post #23

This is dangerous advice. At least on Ubuntu 16.04 with stock kernel running due to memory pressure into swap kills the systems. Basically everything times out and you can only hard-reboot. Without swap you have an OOM at least something you can recover from. If you need swap just add a little like 1-4gb - because if you have spinning disks 64gb swap won't help you at all because every request to swap is magnitudes m…

Not just ubuntu. We tell our engineers to remove their swap partitions because sometimes the test suite will consume all remaining memory and push into swap. If that happens, pretty much the only thing you can do is a hard poweroff. If you're fast, you can Ctrl+C and only lose a minute or two, but often you're just completely stuck.

Much better to OOM and have the test suite killed early.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#30

What do the people that use cloud instances that don't have local disks do? You definitely don't want swap to be on EBS...

Its a good question, but the swap advice is suspicious. If I would normally purchase a 4GB of ram instance with 4GB swap, then what happens when I purchase a 8GB instance? Do I still need 4GB swap? Hopefully this helps illustrate that it is really the applications you intend to run and the swap doesn't mean the system gets more efficient
Post reply on HN