tmpfs as Scratch Space (/dev/shm)
tmpfs as Scratch Space (/dev/shm)
Thesis
tmpfs is a filesystem backed by page cache / RAM (and swap if the kernel must). /dev/shm is the usual shared-memory tmpfs for POSIX shm and a convenient place for fast, disposable scratch files. It is not free disk: fill it and you pressure memory. This chapter inspects mount options and writes a 64 MiB test file to watch capacity move.
Mental model
write(file in tmpfs) → RAM pages (file-backed)
unlink / reboot → pages freed
under pressure → may spill to swap (still not "durable disk")
Worked examples
Case 1: findmnt, write, df
findmnt -t tmpfs -o TARGET,SOURCE,FSTYPE,OPTIONS,SIZE,AVAIL
df -h /dev/shm
dd if=/dev/zero of=/dev/shm/books-tmpfs-test bs=1M count=64 status=none
ls -lh /dev/shm/books-tmpfs-test
df -h /dev/shm
rm -f /dev/shm/books-tmpfs-testOutput from this run:
TARGET SOURCE FSTYPE OPTIONS SIZE AVAIL
/dev/shm tmpfs tmpfs rw,nosuid,nodev 7.8G 7.8G
└─/dev/shm shm tmpfs rw,nosuid,nodev,noexec,relatime 7.8G 7.8G
---
Filesystem Size Used Avail Use% Mounted on
shm 7.9G 0 7.9G 0% /dev/shm
--- write test ---
-rw-r--r-- 1 box box 64M Oct 27 21:08 /dev/shm/books-tmpfs-test
Filesystem Size Used Avail Use% Mounted on
shm 7.9G 64M 7.8G 1% /dev/shm
--- mount options from /proc/mounts ---
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
shm /dev/shm tmpfs rw,nosuid,nodev,noexec,relatime 0 0
rw,nosuid,nodev
rw,nosuid,nodev,noexec,relatime
A 64 MiB write moved /dev/shm from empty to ~1% used on an ~7.9 G tmpfs.
Trap
Using tmpfs for data you cannot rebuild. Power loss or OOM kills the files. Also watch noexec on some mounts — this host shows a nested shm mount with noexec, which breaks running binaries from /dev/shm.
Boring rule
Scratch, locks, and IPC only. Cap size with size= in the mount unit if workloads can balloon.
Try this
- Compare write bandwidth of
/dev/shmvs a disk-backed/tmp. - Read
man tmpfsand your distro’s/etc/fstabor systemd mount unit. - Check whether CI runners mount
/dev/shmsmaller than you expect (often 64 MiB).
Sources
tmpfs(5),shm_overview(7)- systemd
tmpfsmount units