skip to content

A Linux host has a tmpfs filesystem mounted at /dev/shm whose size is reported as half the machine's RAM. What kind of storage is tmpfs, what does that size figure actually mean, and what happens to files written into it?

level: juniorimportance: should knowfreq 52%

answer

  1. memory, not a block device
  2. that number is a ceiling
  3. pages can go to swap
  4. ramfs: no cap, no swap
  5. empty after every reboot

basics

~20 s

tmpfs is a memory-backed filesystem: its files live in the kernel's page cache, may be pushed out to swap under pressure, and disappear on unmount or reboot. The reported size is a ceiling, not a reservation.

solid answer

~40 s

tmpfs has no block device behind it — the kernel stores its files as shared-memory pages, the same pool the page cache uses, accounted as `Shmem` in `/proc/meminfo`. When you mount one without `size=`, the default limit is half of physical RAM, and that number is a maximum: nothing is allocated until files are written, and every byte written really does consume memory, so filling a tmpfs is a way to push a machine towards the OOM killer rather than towards a disk-full error. Unlike `ramfs`, tmpfs pages are swappable and the mount is size-capped. The contents are volatile: unmount or reboot and they are gone. `/dev/shm` is the standard tmpfs used for POSIX shared memory, and `/run` holds volatile runtime state on systemd hosts.

code

bash · 4 lines
bash
mount -t tmpfs -o size=512m,nr_inodes=10k tmpfs /mnt/scratch
dd if=/dev/zero of=/mnt/scratch/blob bs=1M count=64
df -h /mnt/scratch
grep Shmem /proc/meminfo

go deeper

for a junior

Be able to say plainly that a tmpfs lives in memory, that its files are gone after a reboot, and that the size shown is an upper limit rather than space already taken.

for a middle

Explain the mechanics: pages come from the shared-memory pool, are charged as files are written, are swappable, and are not reclaimable by dropping caches the way cached disk pages are.

for a senior

Show the operational consequence — a filling tmpfs is a memory incident, not a storage incident. Talk about sizing the mount deliberately so a runaway writer hits ENOSPC instead of the OOM killer.

for a principal

Own the policy question: which workloads are allowed tmpfs scratch space, how much of the node's memory budget it may claim alongside the application heap, and whether swap is present and encrypted before anyone treats tmpfs as a secrets store.

## What tmpfs actually is Most filesystems are a layout imposed on a block device: ext4 on `/dev/sda1`, XFS on an LVM logical volume. tmpfs has no device. It is a filesystem implemented directly on top of the kernel's virtual-memory subsystem — its file contents are pages of memory, indexed by the same page cache machinery that caches disk files, but with nothing on disk to write them back to. That single fact explains every property people find surprising about it. Because the pages are anonymous-but-shared rather than file-backed, the kernel accounts them as shared memory. You can watch a tmpfs grow directly: ``` grep -E 'Shmem|SwapTotal' /proc/meminfo dd if=/dev/zero of=/dev/shm/blob bs=1M count=256 grep Shmem /proc/meminfo ``` `Shmem` climbs by 256 MiB. No block device is touched. ## The size figure is a ceiling, not a reservation If you mount a tmpfs without options, the kernel's default limit is half of physical RAM. That is a limit, not a claim: a freshly mounted 8 GiB tmpfs consumes essentially nothing. Memory is charged page by page as files are written, and released when files are deleted or truncated. `size=` and `nr_inodes=` set the caps explicitly, and both accept `k`, `m` and `g` suffixes: ``` mount -t tmpfs -o size=512m,nr_inodes=10k tmpfs /mnt/scratch ``` When a tmpfs hits its cap, writes fail with `ENOSPC` — a normal "no space left on device" error, even though the disk is empty. That is the good outcome. The bad outcome is the one people forget: a tmpfs mounted with a generous limit is a legitimate way for an application to consume all of the machine's memory while looking, from `df`, like an ordinary filesystem filling up. ## Swap, and the difference from ramfs tmpfs pages can be written to swap when the system is under memory pressure. That means tmpfs is not a guarantee of RAM-speed access, and it is not a guarantee that data never touches a disk — a subtlety that matters when someone stores secrets or key material in `/dev/shm` and assumes it can never be persisted. On a machine with swap, it can be, unless the swap device itself is encrypted. The older `ramfs` is the opposite bargain: it cannot be swapped and it accepts no size limit, so a runaway writer fills memory until the machine becomes unusable. tmpfs was created precisely to add the size cap and the swap escape hatch, which is why practically everything you meet on a modern system is tmpfs. ## Why dropping caches does not free it A common misconception is that because tmpfs lives "in the cache", the kernel will reclaim it under pressure the way it reclaims cached file data. It cannot. A cached page of an ext4 file has a clean copy on disk, so the kernel can simply drop it. A tmpfs page has nowhere to be dropped to — the filesystem *is* the memory. `echo 3 > /proc/sys/vm/drop_caches` will not shrink a tmpfs by a single byte. The only ways memory comes back are deleting the files or swapping the pages out. ## Where you already have one `/dev/shm` is the mount POSIX shared memory is built on: `shm_open()` creates a file there, and processes map it to share memory. On systemd hosts, `/run` is a tmpfs holding volatile runtime state — PID files, sockets, generated configuration — which is why it is empty again after a reboot. Several distributions also mount `/tmp` as tmpfs. Container runtimes use tmpfs mounts for the same reason, to give a workload scratch space that cannot outlive it. ## When to reach for one The honest use cases are scratch space with a short life and an appetite for IOPS — build intermediates, test fixtures, unpacked archives, caches you would rather rebuild than persist — and data you specifically do not want left on the disk after the process exits. The disqualifier is durability: anything that must survive a crash, and anything larger than you are willing to lose from the machine's memory budget, belongs on a real filesystem. ## What the interviewer is checking They want to hear that you read `df` output for a tmpfs as a memory number, not a storage number. A candidate who says "the /dev/shm mount is 90% full, so we're about to run out of RAM" has understood it. A candidate who says "it's fine, that's just disk" has not.

  • If tmpfs data lives in the page cache, why does writing to /proc/sys/vm/drop_caches never reclaim any of it?
    Because the kernel can only drop a cached page when a clean copy exists somewhere else. A cached ext4 page is backed by the file on disk, so it is discardable; a tmpfs page has no backing store — it is the only copy. The memory comes back only when the file is deleted or truncated, or when the pages are swapped out.
  • What is the practical difference between tmpfs and ramfs?
    ramfs takes no size option and its pages are never swapped, so a process that keeps writing consumes memory until the machine is effectively dead, with no ENOSPC to stop it. tmpfs adds both the size cap and swap backing. That is why tmpfs replaced ramfs for essentially all general use; ramfs survives for a few early-boot and special cases.
  • Someone stores a private key in /dev/shm so it 'never touches disk'. Is that claim true?
    Not by itself. tmpfs pages are swappable, so under memory pressure the kernel may write them to the swap device, where they persist until overwritten. The claim only holds if swap is disabled or encrypted. Also remember that any process with matching credentials can read /dev/shm, so file permissions still matter.

saying these in an interview costs you the question

  • Thinks mounting a tmpfs reserves that much RAM up front
  • Believes tmpfs files never reach a disk under any conditions
  • Reads df output for tmpfs as free disk space
  • Expects tmpfs contents to survive a reboot
  • Thinks drop_caches will reclaim tmpfs memory

context