Can someone answer a naive question about the new zram implementation? I think I've found something important that's been overlooked.
# Use half of the real memory by default --> 1/${ram_divisor}
ram_divisor=2
mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem")
memory_total=$(awk '{printf("%d",$2*1024)}' <<<${mem_info})
mem_per_zram_device=$(( ${memory_total} / ${zram_devices} / ${ram_divisor} ))
for (( i=1; i<=zram_devices; i++ )); do
[[ -f /sys/block/zram${i}/comp_algorithm ]] && echo lz4 >/sys/block/zram${i}/comp_algorithm 2>/dev/null
echo -n ${mem_per_zram_device} > /sys/block/zram${i}/disksize
mkswap /dev/zram${i}
swapon -p 5 /dev/zram${i}
done
echo -e "\n### Activated ${zram_devices} zram swap devices with ${mem_per_zram_device} MB each\n" >>${Log}
Is the intent here to devote half of real memory to zram and take advantage of compression to multiply that chunk of memory, or is the intent to provide some small swap space without hitting swap on flash storage?
Right now the script is doing the latter, not the former. The current implementation makes swap space equal in size to half of ram, which is then compressed (usually at 2:1 or 3:1, most often at 3:1) and stored in memory. This means that, on average, the amount of actual ram used by zram hovers between 25% and ~17% rather than the apparently intended 50%.
I spent quite a bit of time researching and profiling something similar for my own use and I've settled on the following to allocate 50% of real memory to zram:
mem_per_zram_device=$(( 3 * ${memory_total} / ${zram_devices} / ${ram_divisor} ))
Coincidentally this is the same zram allocation that Google makes on ChromeOS.
I've seen the same mistake made in almost every single zram script that I can find via Google so this definitely isn't an Armbian thing. It looks like someone made the original logic error at some point a long time ago and it's been copy-pasted into every zram setup script for the last 7 or 8 years.
For what it's worth setting /sys/block/zramN/mem_limit does what everyone seems to be expecting and limits the amount of ram that the zram device can use. Unfortunately there is no way to adaptively vary /sys/block/zramN/disksize according to the actual compression ratio so the currently accepted best practice seems to be assuming 3:1 and punting (this is what Google does on ChromeOS.)