7

This question queries whether to use /dev/shm or /tmp. Within the answers provided, the general impression arises that /dev/shm is faster than /tmp since it writes to RAM instead of disc. Searching the webs about this topic vaguely confirms this impression. Unfortunately, I can't find a reliable source undoubtedly showing which one is more performant, e.g. by measurement.

For example, this quote from the first answer:

Since RAM is significantly faster than disk storage, you can use /dev/shm instead of /tmp for the performance boost, if your process is I/O intensive and extensively uses temporary files.

The linked article states:

/dev/shm is nothing but implementation of traditional shared memory concept. It is an efficient means of passing data between programs. One program will create a memory portion, which other processes (if permitted) can access. This will result into speeding up things on Linux.

This does neither confirm the answer citing this web site, nor does it compare the performance of /dev/shm vs. /tmp. It is rather a mere description of what /dev/shm is.

Nonetheless, there is one comment in the first answer, stating (as of today):

There is no performance boost by using /dev/shm. /dev/shm is memory (tmpfs) backed by the disk (swap). /var/tmp is memory (disk cache) backed by the disk (on-disk filesystem). In practice, performance is about the same (tmpfs has a slight edge but not enough to matter). /tmp may be tmpfs or not depending on how the administrator configured it. There is no good reason to use /dev/shm in your scripts.

As this comment remains unchallenged, and other answers and comments suggest otherwise, the question remains whether /dev/shm is faster than /tmp or not?

NB: I'm well aware that the answer most probably is like "it depends". There is a question here on SU:SE about which filesystem to use for /tmp. There is also a related question over at SO.

hintze
  • 172

1 Answers1

3

When you use /dev/shm you are not writing directly RAM. It is a tmpfs filesystem, tmpfs keeps all files in virtual memory (a disk as an extension of RAM).

From kernel.org:

If you compare it to ramfs (which was the template to create tmpfs) you gain swapping and limit checking. Another similar thing is the RAM disk (/dev/ram*), which simulates a fixed size hard disk in physical RAM, where you have to create an ordinary filesystem on top. Ramdisks cannot swap and you do not have the possibility to resize them.

Since tmpfs lives completely in the page cache and on swap, all tmpfs pages will be shown as “Shmem” in /proc/meminfo and “Shared” in free(1). Notice that these counters also include shared memory (shmem, see ipcs(1)). The most reliable way to get the count is using df(1) and du(1).

ramfs is a RAM-based filesystem:

Ramfs is a very simple filesystem that exports Linux’s disk caching mechanisms (the page cache and dentry cache) as a dynamically resizable RAM-based filesystem.

Therefore, answering to your question, there is not a performance boost since both (/dev/shm and /tmp) are using the disk.

You can verify when you write in RAM with this example:

mkdir /mnt/ram
mount -t ramfs -o size=2g ramfs /mnt/ram
# Create random file of 2GB
dd if=/dev/urandom bs=1024 count=2000000 of=/tmp/testfile conv=notrunc
# Check ram usage
free
cp /tmp/testfile /mnt/ram
# Check ram usage again
free
sinkmanu
  • 219