Skip to content

Instantly share code, notes, and snippets.

@rizalp
Last active October 22, 2023 12:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rizalp/52f5398b7aae675eb21f693f496e5215 to your computer and use it in GitHub Desktop.
Save rizalp/52f5398b7aae675eb21f693f496e5215 to your computer and use it in GitHub Desktop.
Zram Ubuntu 1804
  • sudo apt install zram-config, this will install several files, including systemd startup /etc/systemd/system/multi-user.target.wants/zram-config.service
  • sudo nano /usr/bin/init-zram-swapping and edit:
#!/bin/sh

# load dependency modules
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))

# initialize the devices
for i in $(seq ${NRDEVICES}); do
  DEVNUMBER=$((i - 1))
  echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm
  echo $mem > /sys/block/zram${DEVNUMBER}/disksize
  mkswap /dev/zram${DEVNUMBER}
  swapon -p 100 /dev/zram${DEVNUMBER}
done
@rizalp
Copy link
Author

rizalp commented Jan 18, 2019

In /etc/sysctl.conf, append:

vm.swappiness = 80

sudo sysctl -p

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