You can set the value of /proc/sys/vm/swappiness
to control the ratio of segments of data swapped to the segments of data kept in memory. A value of 0 completely avoids swapping at all costs.
This can be done using either:
echo 0 > /proc/sys/vm/swappiness
sysctl -w vm.swappiness=0
- Storing that setting in
/etc/sysctl.conf
Generally, using just a little swap is not a bad thing. Free memory can be used for caching data read from disk, and the system can plan ahead for a sudden need of lots of memory by an application.
When too many programs are swapped however, there is a lot of disk related activity during every program switch which really makes everything slow down. Before something can be used, it needs to be loaded back into memory.
Disks reads are horribly slow compared to memory access, as it takes significantly longer for the data to arrive. The system has to schedule the read between the other read/write requests, the drive starts making attempts to find the right cylinder, and finally starts slowly delivering data.
Hence, I think your logic is flawed. Generally, you want to keep programs running in memory, while still keeping enough room for sudden growth. Do not use the swap too often to "write things to disk", because it is neither a backup nor a performance improvement.
Older computers contained less memory and suffered from swapping problems as a result. When many programs were open at once, the system would slow down and you could hear the disk reading and writing in order to the swap file.
Zram creates a block device backed by compressed ram. You can use that block device for swap. Normally memory pressure first results in the cache being discarded, and only after most of the cache has been freed up and memory is still tight does the system start swapping.
CleanCache allows pages from the page cache to be migrated to a back end, such as xen tmem, which is memory managed by the hypervisor and shared between multiple VM guests. The goal of this is to allow multiple VM guests caching the same data to do so using the same ram, instead of each having their own cache with their own copy of the same data.
ZCache is another CleanCache back end. Instead of passing the memory to the hypervisor to hold ( which only applies if you are using a Xen VM environment ), it stores the cache pages compressed in ram, similar to Zram. The difference is that ZCache transparently stores cache pages, but Zram creates a block device that you can use for swap.
If you have memory hungry applications, then you will need swap space to support them, so you will still want to use zram ( likely with a very high swappiness value ). This is because CleanCache only compresses cache pages; application memory has to be sent to swap. If you aren't using all of your memory on applications, then you can use CleanCache with the ZCache backend to make more effective use of the remaining memory for disk caching by compressing the disk cache. You might even use a mix of the two techniques.
Best Answer
Sidenote: because of per-cpu locking, it is important to have as many zram-swaps as CPUs (modprobe zram_num_devices=n zram) instead of a single big one. RTFM!