Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm configuring & compiling new 3.0 kernel. One of the goodies I planned to use for some time (by patching) that was merged into 3.0 is zram.

Is it possible to set both hdd swap and zram swap so the zram is used first and only spilled pages are put into actual swap?

share|improve this question
up vote 5 down vote accepted

swapon have -p switch which sets the priority. I can set up:

swapon -p 32767 /dev/zram0
swapon -p 0 /dev/my-lvm-volume/swap

Or in /etc/fstab:

/dev/zram0              none swap sw,pri=32767 0 0
/dev/my-lvm-volume/swap none swap sw,pri=0     0 0

EDIT: Just for a full solution - such line may be helpful as udev rule:

KERNEL=="zram0", ACTION=="add", ATTR{disksize}="1073741824", RUN="/sbin/mkswap /$root/$name"
share|improve this answer

Sidenote: because of per-cpu locking, it is important to have as many zram-swaps as CPUs (modprobe zram_num_devices=n zram) instead of a single big one. RTFM!

share|improve this answer
[citation needed]? I cannot find the recomendation in Linux documentation or Google. – Maciej Piechotka Nov 28 '11 at 23:28
By default max_comp_streams seems to be 1 according to documentation. I haven't checked if that parameter existed in 2011 but it seems to be a better option then multiple swap files. – Maciej Piechotka May 21 '15 at 19:27
Testing with the package zram-config under Ubuntu (and likely debian) I noted that the configuration defaults to 1 per core or thread (2 on an old E2140 dual core and 4 on an i3-3220) – Elder Geek Feb 29 at 4:03

Select how many zram devices do you want by passing


to kernel parameters or directly to module (without zram.) . Default value is 1. zram will be able utilize as many cpus as number of devices you create.

Prepare zram0 device, set size to 1GB.

echo 1000000000 > /sys/block/zram0/disksize

Alternatively you can do it by udev rule. Create swap on zram0

mkswap /dev/zram0

and enable it with higher priority than other swap devices

swapon -p 32767 /dev/zram0
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.