RAM disk

From Helpful
(Redirected from Zram)
Jump to navigation Jump to search

The lower-level parts of computers

General: Computer power consumption · Computer noises

Memory: Some understanding of memory hardware · CPU cache · Flash memory · Virtual memory · Memory mapped IO and files · RAM disk · Memory limits on 32-bit and 64-bit machines

Related: Network wiring notes - Power over Ethernet · 19" rack sizes

Unsorted: Interrupts · Computer booting · GPU, GPGPU, OpenCL, CUDA notes


A RAM disk refers to, well, taking fast RAM and making it appear like disk-style storage.


Such a RAM disk is often...

  • presented by a program in an already-running system,
  • asking the system for some memory, and
  • somehow making it look like disk storage to others,

often with a regular POSIX/windows-style filesystem interface.



The reason to do this is often that

a program (or command) can only really read and write to disk
operations towards permanent storage are typically slower than operations towards RAM
you go this faster route at the risk of losing everything in there if your computer crashes


This was particularly interesting when the main options were RAM and platter storage, primarily because the disk seek overhead was orders of magnitude higher, and it easily gets factors worse when dealing with multiple readers/writers,


Comparing RAM to SSD is one or two orders of magnitude less difference (than RAM to platter), and doesn't get much worse with multiple readers/writers, so the tradeoff is often not as worth it.

One reason to still do it is probably to try to lessen wear of that flash, especially if you often deal with huge amounts of temporary data (especially if you can just re-generate that).



Note that, because OSes will generally consider all RAM fair game to swap to disk, and because that would defeat the purpose of the speedup, RAM disks may make a point of marking this memory as non-swappable.

...which, depending on further details, may easily amount to reserving and then never using physical memory, so will decrease the amount of freely usable RAM and increase the likeliness of the system's memory pressure.

tl;dr: you may make other things worse, which can be dumb if you then don't use it.


Linux

tmpfs and ramfs; shm and /run

If you want to ruse RAM as a disk, the immediate options in linux are:

  • ramfs is actually very thin wrapper exposing the RAM cacheing system as usable space.
  • tmpfs, based on ramfs, adds a size limit and swappability
(so acts much more like the classical "allocates a fixed block of RAM" style ram disk)


The main differences:

  • size:
neither consumes RAM until you store something
ramfs will grow as far as it can, when asked
tmpfs will report an error when it hits its limit (will act like a full disk) - so is safer for system stability
  • df:
ramfs adds to the 'cached' figure in free/top
tmpfs shows as a drive in e.g. df
  • swap:
ramfs is not eligible for swap
tmpfs is eligible for swap - safer, but can defeat the point somewhat


While ramfs is the sometimes-lower-latency option int that there can be no swapping via possibly-slow IO, it also means it is up to every app that uses it to to behave; "will not swap" combined with " can always grow" means you can very efficiently run yourself out of free RAM, cause trashing and system instability.



Uses

Linuxes keep various transient runtime information in ram disks, largely because there's no point contending for for disk for something you won't ever care to keep.

This includes /var/lock (locks), /var/run (runtime state), /dev/shm (modest data passing), and others that can be distro-specific.

This is usually backed by tmpfs.

Some distros decided to migrate (for now symlink) this stuff to /run so a single mount covers them all - see e.g. [1]

zram

In the linux kernel since 3.2(verify)

zram (previously compcache) provides block devices that are backed by memory and transparently compressed (lzo, lz4, more?).

Uses up to a predefined uncompressed maximum size.

(TODO: figure out whether it's eligible for swap)


This can make sense for

  • scratch space
  • certain caches, e.g. ZFS L2ARC on zram can make sense
  • backing compressed swap, effectively increasing the amount of available RAM when things are swapped out, without using disk to do it
...but these days you probably want to use zswap for that


See also:

zswap

Provides swap pages from RAM, rather than from a block device.

Effectively an layer between RAM and disk swap: if the zswap pool is full, or RAM is exhausted, zswap moves pages to disk swap in an LRU style.


Difference between zswap and zram-which-you-happen-to-use-to-back-swapspace

  • zram-for-swap is a few more commands
  • zram-used-for-swap is separate from existing swap
so zswap is cleverer when there is disk swap
and zram often works better when there won't be other swap configured


See also:

initrd, initramfs

This is a specific use - a bootstrapping thing: the purpose of linux's initramfs (previously initrd) is to mount the root filesystem.


initramfs

See also:


rescueInitramfs

cramfs, squashfs

(squashfs has replaced cramfs)

Read-only filesystem with low overhead, most useful for embedded devices, thin clients, system partitions on phones, liveCD/liveUSB.


In some cases the read-only nature is part of the design and means an embedded device can't easily brick itself, in other cases (e.g. liveUSB) it's mainly to save a little space.

Windows