11

I'm thinking about using zRam on my server. From what I've read about zRam, it apparently does everything tmpfs does and more.

Once zRAM is working, is there any reason to keep tmpfs around? Is there anything tmpfs does that zRAM can't do? Is there ever a reason to use tmpfs instead of or in addition to zRAM?

David Cary
  • 458
  • 5
  • 17

2 Answers2

10

Data written to a tmpfs will be kept in-memory uncompressed as long as enough memory is available. Once the system comes under memory pressure, it tries to move data from the tmpfs (and from any other places, like normal process memory) to any swap (including swap on zram) it can find. Also you can change parameters of a tmpfs by doing a simple mount -o remount, especially the size of the file system or the number of inodes.

Using zram as the backing of a temporary file system has the disadvantage, that all data written there needs to be compressed immediatelly (and decompressed upon use). Also you need to put a file system on top, whiches structures need to be stored even if there are no actual files stored. Also you get limitations like max number of inodes, you can't easily change it's size and so on.

IMHO, best solution is to mount tmpfs somewhere to use and create a zram swap. It combines the advantages of both by minimizing the disadvantages.

Or buy more ram of couse ;)

0

You will get better performance from using zram for swap due to the compression that is possible.

zram-init makes zram simple to use. Remember to read the instructions.

Your kernel needs to be built with CONFIG_ZRAM_LZ4_COMPRESS enabled to be able to benefit from lz4 compression.

I use zram for swap & for /tmp (zram-init uses sensible default mount options for /tmp):

root@alpine [~]# cat /proc/swaps
Filename                Type        Size    Used    Priority
/dev/vda7                               partition   1045500 0   -1
/dev/zram0                              partition   1048572 0   16383
root@alpine [~]# mount|grep zram
/dev/zram1 on /tmp type ext4 (rw,nosuid,nodev,noexec,discard)

There are still use cases for using tmpfs. I use these for bind mounting volumes with secure mount options into lxc containers:

root@http [~]# mount|grep tmpfs
tmpfs on /var/www/domain.com/bin type tmpfs (ro,nosuid,nodev,relatime,size=200k)
tmpfs on /var/www/domain.comm/dev type tmpfs (rw,nosuid,relatime,size=4k,mode=755)