How to manually configure zram on Ubuntu?

(ljpp) #1

I’ve been thinking that zram could be useful on a VPS (or especially dedicated), in order to avoid swapping to the SSD. I’ve actually used it with success on a 1GB VPS and the basic installation is trivial on Ubuntu:

sudo apt-get install zram-config

The zram-config however always allocates 50% of RAM to a number of virtual swap volumes, based on the number of CPU cores. So on a 1GB single-core box it utilizes 1x512MB virtual swap partition for zram. But on a 4GB dual-core configuration the result is 2x1024MB - not ideal.

What I would like to try is to allocate 512mb (2x256) to zram. The compression ratio is typically around 1:3, so this would give about +1GB headroom, thus reducing the need for SSD swapping. This would also be beneficial on laptops or workstations to reduce SSD/HD wear and tear.

But the problem is that sane zram settings documentation is nowhere to be found! I’ve Googled a lot. Anyone familiar with it?

(Jeff Atwood) #2

Not a huge fan of this approach since you’ll be paying CPU time, and Discourse is fairly CPU intensive as a Ruby app, particularly under load. It makes periods of high load much more risky than they would otherwise be, as you’ll be hitting CPU and RAM and potentially even disk much much harder than you ordinarily would.

The only scenario where this makes sense is when you have lots of CPUs (and somehow, not much RAM), and that’d be mostly true on real physical hosting versus cloud in my opinion.

The good news is that outside of load thrashing – which becomes super risky – the performance hit is minimal:

It is also telling, IMO, that Android – which needs to run on super mega low end devices, which is literally the perfect place for this approach – does not do RAM compression at all.

(ljpp) #3

You are probably right that zram is not generally recommendable, but there are some scenarios where it could be useful. I see lots of threads here about people running Discourse on very low end boxes (for development etc) and there zram could be useful. It had a positive impact on my single core 1GB VPS setup, eliminating regular swapping (steady traffic, page views counted in hundreds per day) – but of course it made more sense to switch to a cheaper VPS provider that gave me 2GB at a lower price point. Android does use zram for compression, as does Chrome OS and Windows 10 has a similar approach enabled by default too (low end tablets use it).

Anyway, I would like to experiment with zram on a VPS and I would definitely enable it on my aging laptop, but the default of 50% RAM allocated is too much.

There is a setting file /etc/systemd-swap.conf, but even ArchWiki fails to provide documentation how to use it.

(Javier Lopez) #4

It depends on the Ubuntu version you’re using but in general those parameters are hard-coded, for example in 16.04 the zram partitions are created by invoking /usr/bin/init-zram-swapping, in there you see the line:

mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

Which configures the memory to half the totalmem, so if you want to modify the value you’ll need to edit that file.

(Lutz Biermann) #5

From my experience, there is a much simpler way to achieve lesser swapping.

Just put vm.swappiness=10 into your /etc/sysctl.conf. On very low end hardware use vm.swappiness=1. Don’t forget to reboot after the change.