How to permanently enable compressed ram swap? What version to use?
Solution 1
There's now a PPA that installs a proper Upstart script for enabling zram at boot-time. It chooses the correct size and number of compressed swap devices for your system.
https://launchpad.net/~shnatsel/+archive/zram
Solution 2
I was struggling with the same problem.
Today I found an excellent blog post about it. http://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/
Although "sudo start zramswap" didn't work, when I restarted my PC it solved my problem perfectly.
Try it.
Solution 3
Straight from the Debian wiki. For me, this is the easiest.
First, copy and paste this code into /etc/init.d/zram
### BEGIN INIT INFO
# Provides: zram
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: S
# Default-Stop: 0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description: Use compressed RAM as in-memory swap
### END INIT INFO
# Author: Antonio Galea <[email protected]>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
FRACTION=75
MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`grep -c processor /proc/cpuinfo`
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))
case "$1" in
"start")
param=`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '`
modprobe zram $param=$CPUS
for n in `seq $CPUS`; do
i=$((n - 1))
echo $SIZE > /sys/block/zram$i/disksize
mkswap /dev/zram$i
swapon /dev/zram$i -p 10
done
;;
"stop")
for n in `seq $CPUS`; do
i=$((n - 1))
swapoff /dev/zram$i && echo "disabled disk $n of $CPUS" &
done
wait
sleep .5
modprobe -r zram
;;
*)
echo "Usage: `basename $0` (start | stop)"
exit 1
;;
esac
Next, execute these two commands:
sudo chmod +x /etc/init.d/zram
sudo /etc/init.d/zram start
Finally, to add zram at startup:
sudo update-rc.d zram defaults
Done.
Solution 4
Here's the cheap solution. Add the following line to /etc/rc.local
, before the exit 0
:
find /dev/ -maxdepth 1 -name 'ramzswap*' | while read dev; do
mkswap $dev
swapon -p 1000 $dev
done
Related videos on Youtube
David Guo
Updated on September 18, 2022Comments
-
David Guo over 1 year
EDIT: In precise there's now
zram-config
. It's an upstart job compressing up to half of your ram spread over $(number of CPU cores) swap devices. It didn't allways start at boot but issuingsudo service zram-config start
works.I enabled compcache="256 M" in
/etc/initramfs-tools/initramfs.conf
as described here (by me :P). This - I believe - creates/dev/ramzswap0
but it is never enabled as swap. It works only aftermkswap
&&swapon
.Then there is the module zram that creates
/dev/zram
. Is it something else? It works the same way but/dev/ramzswap
is created from the module ramzswap.At the end of the day I wanna have a compressed swap in ram and use the better of the two and for that I need to know how to enable it permanently in a non hackish way. How is this done?
I wrote about ramzswap in Lucid here but things have changed in Natty. You can still enable ramzswap in initramfs.conf but it doesn't get activated.
P.S.:I scanned all udev rules in /lib and/etc but found nothing of interest.
-
David Guo almost 13 years
for i in /dev/ramzswap*;do ...
would the better solution I think. Still a tad too hackish in my book. -
David Guo over 12 yearsThat's actually the first time I even noticed that there is /etc/init/. Normally I use /etc/init.d/ for starting stuff at boot. Fascinating...
-
David Guo about 12 yearsthere's also zram-config in precise now.
-
Ryan C. Thompson about 12 years
/etc/init/
is where Upstart init scripts live. The ones in/etc/init.d
are mostly just compatibility wrappers that call the ones in/etc/init
. -
NoBugs almost 11 yearsWhy was it removed from Raring 13.04?
-
Ryan C. Thompson almost 11 yearsThe PPA probably hasn't been updated.
-
Cbhihe over 7 yearsHow about
find /dev/ -maxdepth 1 -name 'ramzswap*' -print0 | while read -d0 dev; do ...
? It'll obviate the problem of strange filename with newline in it. -
Cbhihe over 7 years@turbo: old stuff here, but I believe Ryan's answer above is actually more general and safer from a scripting viewpoint than what you propose in yr comment. Generally speaking, yr for loop might cause trouble for file names with space and or special characters. This being said you do spare your system a process.