RAM alternative to swap partion or swap file on an SSD

ramramdiskswap

My Debian stretch system has a lot of RAM. Currently, it uses an encrypted swap partition on the SSD, but under normal conditions I don't ever expect lack of memory to cause it to be used.

But turning off swap completely is a bad idea according to the comments of Snakedoc here:

Just note, it's never "safe" to fully disable swap. Even with systems
that have 96+ GB of ram, running at least 512MB of swap is a good
safety measure. It gives your system time to kill unnecessary
processes if something runs amuck and eats up all the ram
unexpectedly… the alternative with swap disabled is to kernel panic.

How to safely turn off swap permanently and reclaim the space? (on Debian Jessie)

So I thought I might reserve some of the ram, say 0.5G for a 'RAM disk', and use that as a swap file, saving writes to the SSD.

How do I do that? Is it a bad idea for some reason?

Best Answer

I wouldn't say its reason is stability, it is a common misconception. If a system ram + swap is full, you will find actually the same symptoms as without swap:

  1. Unexplained, big slowdowns (the system locks the processes asking for ram up for a while, maybe somebody frees the ram required for them)
  2. Processes killed by the OOM (out of memory) killer.

The only difference that without swap, these symptoms happen suddenly, while with swap, they happen only after the swap is filled out. Thus, you have a much bigger delay time before your system begins to be perfectly unresponsive.

This is the first reason, why is it useful to have swap.


The second is lesser known, but actually it is much more important:

In general, having swap doesn't slow your machine down, it accelerates it, even in you have far than enough RAM.

Its reason is the following: what does your system with the RAM actually not used by your processes? They are used to cache your hard disks, making your system faster with it.

If you have swap, you essentially allows your system to swap out the really rarely memory pages, and use their space for disk cache. How fasten do they happen, it depends on your VM settings and heuristics, but they are really good (as far I know, linux is far the best in this sense).


If your hard disks are on SSD, doesn't really influnece these both arguments (although having much faster and zero seek-time disks of course accelerates everything).

I mostly use the old golden rule for swap partitions: I use swap 2x more as my RAM. This is for to avoid swap fragmentation (blocks on the swap file can just so fragment as files on a filesystem). In your case, where you have a tremendous amount of RAM, but costly hard disk with zero seek time, swap fragmentation is not an issue, thus maybe also lesser would be enough. I don't know, how many SSD do you have, maybe 1x or even 0.5x times of your RAM site would be enough.


In my opinion, this "have at least 512M swap" advice is bad, the amount of swap you use have to depend on your system parameters (disk cost, RAM amount, disk amount, hard disk seek time, etc). This 512M is not a "golden rule", it is a "magic number", which is meaningless if we use it in general.

Related Question