Live data from Hacker News

Origin of the rule that swap size should be 2x of the physical memory

retrocomputing.stackexchange.com

31–40 of 78 posts

Re: Origin of the rule that swap size should be 2x of the physical memory

#31

Curious, How much swap have you personally allocated on your personal setup?

16gb.

Fwiw you’ll see technical reasons for swap being a bad idea on servers. These are valid. Virtualised servers don’t really have great ways to make swap work.

On a personal setup though there’s no reason not to have swap space. Your main ram gets to cache more files if you let the os have some space to place allocated but never actually used objects.

As in ‘I don’t use swap because i don’t use all my ram’ isn’t valid since free ram caches files on all major OS’s. You pretty much always end up using all your ram. Having swap is purely a win, it lets you cache even more.

Re: Origin of the rule that swap size should be 2x of the physical memory

#32

in 1997 people talking about it on the Slackware Usenet group: >Question: Why do you need 500MB of swap space? You would be better of >spending your money on more RAM than wasting it on so much swap space, >considering that it would most likely never be used anyways. I work with systems that have between 256MB and 1GB of RAM and between 4GB and 16GB available for Linux. My experience with other operating systems is t…

As has been mentioned a few times in other comments here, I don't believe that's correct. Swap space is not just for "using more memory than you have RAM".

Re: Origin of the rule that swap size should be 2x of the physical memory

#33
post #5

Curious, How much swap have you personally allocated on your personal setup?

Zero. My office workstation has 48 GB of RAM, my home computer has 64 (I went a bit overboard). I have very bad memories of swap thrashing and the computer becoming totally unresponsive until I forced a reset; if I manage to fill up so much RAM, I very much prefer the offending process to die instead of killing the whole computer.

I have 64GB of RAM and 16GB of swap. Swap is small enough it can't get really out of hand.

I have memories from like 20 years ago that even when I had plenty of RAM, and plenty of it was free, I would get random OOM killer events relatively regularly. Adding just a tiny bit of swap made that stop happening.

I'm like 90% sure at this point it's just a stupid superstition I carry. But I'm not gonna stop doing it even though it is stupid.

Re: Origin of the rule that swap size should be 2x of the physical memory

#34
post #28
post #16

Earlier quoted context omitted.

I thought the modern advice was you don't need it at all. No more spinning disks, so the there's no speed gain using the inner-most ring, and modern OSes manage memory in more advanced, and dynamic ways. That's what I choose to believe anyway, I don't need anymore hard choices when setting up Linux :)

I don't think that's correct. Having swap still allows you to page out rarely-used pages from RAM, and letting that RAM be used for things that positively impact performance, like caching actually used filesystem objects. Pages that are backed by disk (e.g. files) don't need that, but anonymous memory that e.g. has only been touched once and then never even read afterwards should have a place to go as well. Also, wit…

Every time I've ran out of physical memory on Linux I've had to just reboot the machine, being unable to issue any kind of commands by input devices. I don't know what it is, but Linux just doesn't seem to be able to deal with that situation cleanly.

Re: Origin of the rule that swap size should be 2x of the physical memory

#35
post #16

Earlier quoted context omitted.

Nope. Those are not the only answers I am seeing. I’m still curious though. 2x was nice because nobody really questioned it. Now that we have there doesn’t seem to be one “answer”. This is a fun/interesting question that comes up every now and then here and elsewhere :-) I suspect someone smarter than me about system tuning will have a much smarter and nuanced answer than “just use 2x”

I thought the modern advice was you don't need it at all. No more spinning disks, so the there's no speed gain using the inner-most ring, and modern OSes manage memory in more advanced, and dynamic ways. That's what I choose to believe anyway, I don't need anymore hard choices when setting up Linux :)

It’s still beneficial so that unused data pages are evicted in favor of more disk cache

Re: Origin of the rule that swap size should be 2x of the physical memory

#36

Curious, How much swap have you personally allocated on your personal setup?

16gb. Fwiw you’ll see technical reasons for swap being a bad idea on servers. These are valid. Virtualised servers don’t really have great ways to make swap work. On a personal setup though there’s no reason not to have swap space. Your main ram gets to cache more files if you let the os have some space to place allocated but never actually used objects. As in ‘I don’t use swap because i don’t use all my ram’ isn’t v…

Having everything in RAM might also be better for security, unless you're encrypting the swap partition.

The contents of swap could be read after a power cut.

Re: Origin of the rule that swap size should be 2x of the physical memory

#37

Curious, How much swap have you personally allocated on your personal setup?

16gb. Fwiw you’ll see technical reasons for swap being a bad idea on servers. These are valid. Virtualised servers don’t really have great ways to make swap work. On a personal setup though there’s no reason not to have swap space. Your main ram gets to cache more files if you let the os have some space to place allocated but never actually used objects. As in ‘I don’t use swap because i don’t use all my ram’ isn’t v…

But then you're putting data that used to be on RAM on storage, in order to keep copies of stored data on RAM. Without any advance knowledge of access patterns, it doesn't seem like it buys you anything.

Re: Origin of the rule that swap size should be 2x of the physical memory

#38

Earlier quoted context omitted.

It's funny how people think they're disabling swapping just because they don't have a swap file. Where do you think mmap()-ed file pages go? Your machine can still reclaim resident file-backed pages (either by discarding them if they're clear or writing them to their backing file if dirty) and reload them later. That's.... swap. Instead of achieving responsiveness by disabling swap entirely (which is silly, because e…

mmap is not swap. It's using the same virtual memory mechanisms to load/dump pages to disk. The policy for when to read and write those pages is completely different.

When the room for memory mapped files gets low enough you get bad thrashing anyway, so the policy difference isn't that important.

Having no swap limits how much you can overburden your computer, but you also hit problems earlier. Here's some example numbers for 64GB of memory: With swap you can go up to 62GB of active program data (85GB allocated and used) before you have performance issues. Without swap you can go up to 45GB of active program data (63GB allocated and used) before you hit a brick wall of either thrashing or killing processes. The no-swap version is better at maintaining snappiness within its happy range, but it's a tradeoff.

Re: Origin of the rule that swap size should be 2x of the physical memory

#39

Earlier quoted context omitted.

It's funny how people think they're disabling swapping just because they don't have a swap file. Where do you think mmap()-ed file pages go? Your machine can still reclaim resident file-backed pages (either by discarding them if they're clear or writing them to their backing file if dirty) and reload them later. That's.... swap. Instead of achieving responsiveness by disabling swap entirely (which is silly, because e…

mmap is not swap. It's using the same virtual memory mechanisms to load/dump pages to disk. The policy for when to read and write those pages is completely different.

It is doing exactly what swap is doing. That it's swap with a different policy doesn't make it not-swap.

Also, that separate policy shouldn't even exist. For LRU/active-list/inactive-list purposes, why does it matter whether a page is anonymous or file-backed? If you need it, you need it, and if you don't, you don't. No reason for anonymous and file-backed memory to be separate sub-sub-systems under vm.

Re: Origin of the rule that swap size should be 2x of the physical memory

#40
I think more people should know about the existence of ZRAM on modern Linux distributions. It's really changed the way I look at swap configs.

ZRAM is a compressed block device that is stored in RAM. It's great!

Previously, if I ever had high memory pressure situations, I really dreaded the slowdowns. Now, with swap sitting on top of /dev/zram0 it's a completely different experience.

I have ZRAM enabled on all of my personal machines, both laptops with limited memory, and desktops with 64 or 128GB of RAM. It's rarely used, but it is nice to have that extra room sometimes.

The performance of a zram device is so much faster than even the latest NVMe drives.

Post reply on HN