Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ZRAM config for Raspberry Pi 3
#!/bin/bash
# Raspberry Pi ZRAM script
# Tuned for quad core, 1 GB RAM models
# put me in /etc/init.d/zram.sh and make me executable
# then run "sudo update-rc.d zram.sh defaults"
modprobe zram
echo 3 >/sys/devices/virtual/block/zram0/max_comp_streams
echo lz4 >/sys/devices/virtual/block/zram0/comp_algorithm
echo 268435456 >/sys/devices/virtual/block/zram0/mem_limit
echo 536870912 >/sys/devices/virtual/block/zram0/disksize
mkswap /dev/zram0
swapon -p 0 /dev/zram0
sysctl vm.swappiness=70
@SonicDMG

This comment has been minimized.

Copy link

@SonicDMG SonicDMG commented Nov 20, 2017

Thank you so much for doing the work to figure this out and create the script. This has been a life saver for the project I'm working on.

@zsmithr1

This comment has been minimized.

Copy link

@zsmithr1 zsmithr1 commented Apr 5, 2018

I second that, works great on mint I am now testing on osmc.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 15, 2018

I secound that. Thanks for this little script.

@mfranzs

This comment has been minimized.

Copy link

@mfranzs mfranzs commented Apr 25, 2018

This was useful - thanks! Is there a way to turn it off once I don't need it anymore? I don't really understand what it's doing.

@sagdsgfdgd

This comment has been minimized.

Copy link

@sagdsgfdgd sagdsgfdgd commented May 30, 2018

it is increasing your swap.

@aguegu

This comment has been minimized.

Copy link

@aguegu aguegu commented Jun 2, 2018

@mfranzs You can disable it by

swapoff /dev/zram0
modprobe -r zram
@aguegu

This comment has been minimized.

Copy link

@aguegu aguegu commented Jun 2, 2018

@zertyz

This comment has been minimized.

Copy link

@zertyz zertyz commented Nov 28, 2018

You might want to update your script to benefit from the new zram backing device -- where uncompressible pages get stored without agressive flushing. I'm getting wonderful results -- better yet when combining parallel backing devices. Here is what I use on my setup of an sd card plus 4 cheap usb pendrives with a dedicated partition to serve as the backing device on each:

modprobe zram num_devices=5; ((z=0)); for d in /dev/mmcblk0pX /dev/sdaX /dev/sdbX /dev/sdcX /dev/sddX; do echo $d >/sys/block/zram${z}/backing_dev; echo lz4 >/sys/devices/virtual/block/zram${z}/comp_algorithm; echo 96m >/sys/devices/virtual/block/zram${z}/mem_limit; echo 512m >/sys/devices/virtual/block/zram${z}/disksize; mkswap /dev/zram${z}; swapon -p 15 /dev/zram${z}; ((z++)); done

Notes:

  1. If not set, max_comp_streams will default to the number of CPUs, which is the optimum setting
  2. Those particular numbers are good for a 512mb ram old raspberry Pi.
  3. Swapping in this way attended to the requisites for my server software -- and also for compiling and testing it. Didn't test on a desktop setup.
@bcutter

This comment has been minimized.

Copy link

@bcutter bcutter commented Mar 22, 2019

When "sudo update-rc.d zram.sh defaults" I get:

insserv: warning: script 'zram.sh' missing LSB tags and overrides
insserv: There is a loop between service watchdog and zram.sh if stopped
insserv:  loop involving service zram.sh at depth 2
insserv:  loop involving service watchdog at depth 1
insserv: Stopping zram.sh depends on watchdog and therefore on system facility `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header

So... what now?

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