This is interesting. Someone over at Linux Journal is using “zram” to turn half the Pi’s RAM into a swap space. They even claim to have removed the swap file entirely, which should improve the life of your SD cards.
Most of the work was done using a script from extremeshok.com (which isn’t always up). I’ve included the complete post text here just in case they both disappear!
SD cards are getting better every year when it comes to wear leveling and write cycles. The concern is always that excessive use of the SD card will wear it out, and it will become read-only. Because the RPi also suffers from limited and non-expandable RAM, the issue of swap space is highly debated.
Raspbian comes installed by default with a swap file active—not a swap partition, but a swap file. Whether this swap file will wear out your SD card is still up for debate. I tend to be paranoid about such things, so I searched for an alternative. Because I have the 512MB units, I have a little more RAM to work with, so I decided to use zRam to compress some of the system RAM and use it as swap space. How does that work? Basically, zRam allows the system to use RAM as swap space, but in a compressed mode that allows it to appear as more RAM+swap than the system actually contains.
zRam already is compiled into the Raspbian kernel, so if you want to try my method, it just takes a little scripting. I absolutely love the script I found at the eXtremeSHOK.com Web site. That site is off-line at the time of this writing, so I’ve included the script below. Create a file called /etc/init.d/zram, which contains the following:
#!/bin/bash
### BEGIN INIT INFO
#Provides: zram
#Required-Start:
#Required-Stop:
#Default-Start: 2 3 4 5
#Default-Stop: 0 1 6
#Short-Description: Increased Performance In Linux With zRam
#(Virtual Swap Compressed in RAM)
#Description: Adapted for Raspbian (Rasberry pi) by eXtremeSHOK.com
#using https://raw.github.com/gionn/etc/master/init.d/zram
### END INIT INFO
start() {
mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
modprobe zram
sleep 1
#only using 50% of system memory, comment the line
#below to use 100% of system memory
mem_total_kb=$((mem_total_kb/2))
echo $((mem_total_kb * 1024)) > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -p 100 /dev/zram0
}
stop() {
swapoff /dev/zram0
sleep 1
rmmod zram
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
Then you need to make the init script executable and force it to start automatically. The following commands accomplish that:
sudo chmod +x /etc/init.d/zram
sudo update-rc.d zram defaults
Basically, as written, the script will use half the available RAM as compressed swap space. Again, all credit goes to the folks at eXtremeSHOK.com for creating the init script. I’ve been very happy with the performance, and it enabled me to get rid of the swap file altogether.
Hopefully someone will find this information useful. Here’s a link to the article at Linux Journal.
Hmmm its an interesting idea but I’m not convinced. You end up sacrificing half your ram (which is scarce anyway) and also using up valuable CPU cycles to save an SD card which by the time it actually dies will only cost a few pence to replace?
I guess it depends on what you are doing with your Pi, if memory and CPU aren’t an issue but you want to run 24×7 for years I guess it has an application?
Oh, I’m not convinced either. 512mb is very little to play with anyway, what with sharing it between main and graphics. Just found it interesting that you _could_ do it. Would love to see some benchmarks, though, if only for media centre Pis.
I tried it and tested FPS with OpenCV webcam viewing in a simple QT window. FPS before zram: 29.4 After zram: 15.6. So now I have to figure out how to remove it again. 🙂
Ew, that’s not good!
Thank you for your test! 🙂
sudo /etc/init.d/zram stop
Wonderful idea, it worked for me also. Thanks with regards