0

I am running 16.04 on a laptop with a 1 GB SSD with 20 GB ram. File system is btrfs and I didn't create swap. It has not caused me any problems for more than a year. However, I would like to try out using ZRAM for swap and read the followning here: https://seravo.fi/2016/perfect-btrfs-setup-for-a-server

"To install zram simply run: apt install zram-config. After next reboot there will automatically be a zram device that the system uses for swapping. It does not matter how much RAM a system has, because at some point the kernel will anyway swap something our from active memory to swap to use the active memory more efficiently."

So I did that and when I do cat /proc/swaps after a reboot I get:

  Filename                                Type            Size    Used    Priority
/dev/zram0                              partition       2553148 0       5
/dev/zram1                              partition       2553148 0       5
/dev/zram2                              partition       2553148 0       5
/dev/zram3                              partition       2553148 0       5

So I now seem to have swap in ZRAM. What I wonder is if this is all I need to do. I do not need to create and enable a swapfile in ZRAM? I am asking since I read here on Ask Ubuntu: ZRAM vs. regular swap partition

that, quote: "You can then format it for swap with mkswap /dev/zram0 and enable swapping on it with swapon /dev/zram0."

It's a bit confusing since it according to the first site I referenced all I need to to is enable ZRAM and the system will use it while it on AskUbuntu seems like I will have to create and enable swap in ZRAM.

Could someone please clarify this?

2 Answers 2

2

All you need to do is to install the zram-config package, which, as the name implies, configures zram for you. cat /proc/swaps is accurate: zram is already active as swap.

The other question is about zram, not the zram-config package.

1
  • Not sure I really understand the difference between zram and the zram-config package. but the answer is good, so thank you!
    – Christian
    Mar 5, 2017 at 3:18
1

ZRAM is simply a compressed ramdisk driver. When you enable it, you get block devices, /dev/zram[0-9]+, and all data stored in this block devices will be compressed and stored in memory.

Since it is just a block device, you can use it to store anything, like a file system or swap space—it is not particularly useful to store partitions, since when partitioned it won’t show new devices for each partition like your /dev/hd[a-z] or /dev/sd[a-z] etc will.

So, to use it for swapping, you must first format it with mkswap, and then make Linux swap on it with swapon. zram-config is just the official Ubuntu package that does this automatically for you on boot (since it is a ramdisk, all data stored in it vanishes when you reboot). There are other scripts that will do the same. I particularly like systemd-swap,⁽¹⁾ which I use in both my ArchLinux box and my Raspberry Pi (which runs a Debian-like distro), and which handles auto-swapping on disk-based swaps, ZRAM and Zswap. I’m quite sure it works with Ubuntu also.

As a curiosity, this is how zram behaves in regard to memory usage: while the maximum amount of data you can store in this device is fixed, the amount of memory it uses varies according to how compressible the data is. For example:

% sudo swapoff /dev/zram0
% sudo dd if=/dev/zero of=/dev/zram0 bs=1M
dd: writing to '/dev/zram0': No space left on device
243+0 records in
242+0 records out
254492672 bytes (254 MB, 243 MiB) copied, 6.10473 s, 41.7 MB/s
% free -h
              total        used        free      shared  buff/cache   available
Mem:           970M         47M        793M         25M        128M        848M
Swap:            0B          0B          0B
% sudo dd if=/dev/urandom of=/dev/zram0 bs=1M
dd: error writing '/dev/zram0': No space left on device
243+0 records in
242+0 records out
254492672 bytes (254 MB, 243 MiB) copied, 13.2982 s, 19.1 MB/s
% free -h
              total        used        free      shared  buff/cache   available
Mem:           970M        292M        549M         25M        128M        603M
Swap:            0B          0B          0B

So, while my zram device has 254 MB of space, when filled with zeros, it uses almost no memory at all, while when filled with highly incompressible random data, it uses 254M of memory.


⁽¹⁾ https://github.com/Nefelim4ag/systemd-swap

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .