Compressed RAM as only swap, malloc or swap?

If I plan to use a compressed memery disk as the only swap on my machine, no zvol or freebsd-swap partition, how should I create it?
mdconfig -a -t malloc -o reserve -o compress -s 2g -u 0
or
mdconfig -a -t swap -o compress -s 2g -u 0
?
 
The question is, does it make sense to compress swap, since swap should be as fast as possible, so it does not slow down your machine more than needed, and compression reduces access speed.

You see, on a common storage device (HDD, SSD) the computation needed for compression/decompression is by magnitudes faster than the writing/reading to/from such a drive, so it doesn't carry much weight really.
But since the access to RAM is (what? thousand, ten thousand times?) faster than the fastest drive, in that case the computing time for compression/decompression is (way) larger than the read/write access to RAM.

Additionally you need to see, under common circumstances swap is a reserve when there is not enough RAM.
As long as there is enough RAM left, the OS does not swap, because swapping cost time, so reduces the working speed of the machine. So far the idea to place swap on the fastest storage available. But you may reconsider if in your case a RAM disk was a good idea. There can be reasons to do so, but for common situations I dare to doubt that.
If your swap is used at all, then it's because at this moment you are demanding more programs and data to be processed by the machine than fitting into RAM.
When swap is never used, you simply do have more than enough RAM.
When swap is used occasionally, then this is also okay. It just means, sometimes you do heavy load jobs, and then you can live with it.
But if swap is used frequently, or maybe even your swap is always in usage, than this is a sign you simply do not have enough RAM for what you're doing with the machine.
If so you either see how you can reduce the load, e.g. by using fewer programs at the same time (e.g. not editing a large document with lots of pictures with LibreOffice while having four dozens of browser tabs open and compiling a large C++/Rust software and converting a HD movie from one format to another all at the same time 😁) or chose more lightweight programs for the same job (e.g. using a lightweight WM instead of a large DE like KDE for example.)
Or add more RAM to your machine.

Anyway, placing swap space on a RAM disk into RAM seems at the first glance to be a good idea, since it was the fastest memory. But at the same time this reduces your RAM available for the OS's usage - by the amount of that RAM disk, so it enlarges the possibility the OS needs to swap, which bottom line slows down your machine instead of speeding it up.
 
Back
Top