6

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?

  • I think Snakedoc is wrong (in the part you quoted). Without swap, OOM killer will still have time to kill processes. Now, it may "miss" and kill the wrong process(es) but it will continue to kill processes until the memory problem is gone. Many people consider this "bad" because you might end up losing precious data. I myself do a regular back up so I think it's totally fine and I wouldn't want to be slowed by Swap. It hurted my systems much more than helped, so far. Also notice that modern SSD's last longer than HDD, you shouldn't be concerned with writes. Some SSD's come with a 5 years ... – thermomagnetic condensed boson Apr 22 '17 at 21:26
  • (continuation) warranty, while HDD usually come with "only" 3 years of warranty. My systems with swap have been slowed down (unresponsive) as soon as swap was used, even if only a few kB. Such problem never occured yet on my 16 GB of RAM no-swap machine. – thermomagnetic condensed boson Apr 22 '17 at 21:27
  • Using ram disk as swap partition is totally meaningless, not useful. I said very clearly, what is this whole thing about, instead of creating braindamaged ideas, simply create a swap partition 2x times of your available physical RAM, and use it, that is all. I don't think I should convince you to not be dilettant, furthermore I don't think I should teach you against your own will. More time I don't have to your problem, build a trash as you will, good bye. – peterh Apr 22 '17 at 22:36
  • If you somehow want to have an intact SSD without a swap partition, yes then you can live without swap, or you can put in another (not surely SSD) hard drive into your system. Your system will work, and yes, it won't be so good as the one I described, but it will work. I described the disadvantages in my post (lockups & sudden degradation in the case of RAM shortage, fewer disk cache space). There is also a possibility to use a small, but very fast ssd disk as the cache of a big, slow hdd (man7.org/linux/man-pages/man7/lvmcache.7.html), you can then use a part of this two-layer block – peterh Apr 22 '17 at 22:49
  • device partially for swap, too. This whole idea about "I have a fast system with many ram, so I don't need swap" is the typical signature of dilettantism, if you understood my post, you already know, why. But yes, if you have many ram, it will work, and yes, it won't be so fast as with swap. Now really bye. – peterh Apr 22 '17 at 22:50
1

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.

  • M experience is quite different from what you described. I have 2 laptops with a swap partition (and 4 GB of RAM each) and 1 desktop PC with 16 GB of RAM and no swap partition. On the laptops, as soon as I start using the swap partition, the system freezes for about 5 minutes, being totally unresponsive (even with a few kB of swap used, out of 4 GB). I never ever had any slowdown yet on my desktop PC (haven't ran out of RAM yet). Swap hurted me much more than no swap, until now. – thermomagnetic condensed boson Apr 22 '17 at 21:18
  • 2
    @no_choice99 4GB is quite a few for a today desktop system, your laptops use probably much more swap as a few kbytes. Btw, swapping doesn't start in such small sizes, typically the system starts to allocate at least MB-range blocks of swap space. And your laptops are slow because 4GB is few for them, even with swap. But if you would turn off even the swap, they would be much more slow. – peterh Apr 22 '17 at 22:26
  • @no_choice99 If you want fast laptops, try to extend their memory to at least 8GB, switch their hard disks SSD ones, and yeah, use at least 2x their RAMs as swap. And learn to read the swap space usage correctly... cat /proc/swap, it is in kilobytes. – peterh Apr 22 '17 at 22:28
0

Using an HDD for swap on an SSD system is a bad idea. Not only it's slower in speed, it has much longer access time, is far more power hungry and possibly less reliable than a modern SSD. Nowadays SSDs have 5 years warranty and can easily last for 8-10 years even if you write hundreds of GBs to it every day

But it'd be even much better to use zram, possibly along with a small backup swap partition on the SSD. Various Linux distros as well as ChromeOS and Android and have enabled zram by default for years. There's not even the conventional swap by default on Android. Even Windows and macOS nowadays uses compressed RAM before resorting to swap

Since the CPU can decompress data much faster than the HDD, you can clearly feel the system responsiveness once the system begins to swap. The difference is not that inherent on SSDs, but according to the benchmark results here zram with the default lzo algorithm still seems faster. You can easily change to lz4 algorithm which is nearly 5 times faster than lzo with a slightly lower compression ratio

There's also zswap which uses a different mechanism, but still stores compressed data on RAM

Further reading

-1

Given the answer by @peterh, probably a good solution is to stick any old HDD in the machine and use it as scratch space, including swap. That way all ram can be available for the system, even the last 0.5G. A laptop HDD likely would be more energy efficient than a 3.5" drive.

  • 2
    I think this is a bad tip. Modern SSD's can support many more writes than HDD and are much faster. They are also more energy efficiant than HDD. Why would one recommend using an HDD for swap instead of a SSD is beyond my understanding. – thermomagnetic condensed boson Apr 22 '17 at 21:21
  • If you don't want to have a swap space on your ssd on some reason, yes it will work also on the hdd, and yes it will be slower. But if you have probably much more ram as your system needs, it will be ok. It can be particularly usable if your ssd is small, and you won't further shrink it with the swap space. – peterh Apr 22 '17 at 22:53
  • what kind of integrity checking and repair goes on during the swap process? – user87218721 Apr 22 '17 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.