Skip to content

Instantly share code, notes, and snippets.

@Manouchehri
Last active August 2, 2023 16:11
Show Gist options
  • Save Manouchehri/cbc57183ee9f6732a151f7ebe7349067 to your computer and use it in GitHub Desktop.
Save Manouchehri/cbc57183ee9f6732a151f7ebe7349067 to your computer and use it in GitHub Desktop.
sudo dd if=/dev/zero of=/swapfile0 bs=1M count=4096 status=progress # not right for btrfs? Should disable compression too?
sudo chmod 600 /swapfile0
# old kernels need more swapfiles
sudo lvcreate -l +100%FREE ubuntu-vg -n zram0
# edit the other two files in this gist first
sudo /usr/bin/init-zram-swapping
sudo sysctl -p
# You only need one zram device for kernels newer than 3.15
sudo lvcreate -L 500M ubuntu-vg -n zram1
sudo lvcreate -L 500M ubuntu-vg -n zram2
sudo lvcreate -L 500M ubuntu-vg -n zram3
sudo lvcreate -L 500M ubuntu-vg -n zram4
sudo lvcreate -L 500M ubuntu-vg -n zram5
sudo lvcreate -L 500M ubuntu-vg -n zram6
sudo lvcreate -L 500M ubuntu-vg -n zram7
sudo lvcreate -L 500M ubuntu-vg -n zram8
sudo lvcreate -L 500M ubuntu-vg -n zram9
sudo lvcreate -L 500M ubuntu-vg -n zram10
sudo lvcreate -L 500M ubuntu-vg -n zram11
#!/bin/sh
# load dependency modules
# NRDEVICES=$(grep -c ^processor /proc/cpuinfo | sed 's/^0$/1/')
# We don't need multiple devices anymore
NRDEVICES=1
if modinfo zram | grep -q ' zram_num_devices:' 2>/dev/null; then
MODPROBE_ARGS="zram_num_devices=${NRDEVICES}"
elif modinfo zram | grep -q ' num_devices:' 2>/dev/null; then
MODPROBE_ARGS="num_devices=${NRDEVICES}"
else
exit 1
fi
modprobe zram $MODPROBE_ARGS
# Calculate memory to use for zram (1/2 of ram)
totalmem=`LC_ALL=C free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/ *.*//'`
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))
# To override the auto size
mem="4G"
# initialize the devices
for i in $(seq ${NRDEVICES}); do
DEVNUMBER=$((i - 1))
swapdev="$(losetup --find "$(readlink -f /swapfile${DEVNUMBER})" --show)"
echo $swapdev > /sys/block/zram${DEVNUMBER}/backing_dev
echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm
echo $mem > /sys/block/zram${DEVNUMBER}/disksize
echo all > /sys/block/zram${DEVNUMBER}/idle
echo idle > /sys/block/zram${DEVNUMBER}/writeback
mkswap /dev/zram${DEVNUMBER}
swapon -p 5 /dev/zram${DEVNUMBER}
done
vm.swappiness = 80
@Manouchehri
Copy link
Author

Proof of how amazing this is:

dave@xps:~$ zramctl 
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4           7.7G   7G  2.8G    3G      12 [SWAP]
dave@xps:~$ free -mh
              total        used        free      shared  buff/cache   available
Mem:            15G        5.8G        5.2G        1.3G        4.3G        7.9G
Swap:           31G         11G         19G

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment