/tmp vs. /dev/shm for temp file storage on Linux?

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

/dev/shm is intended for a very special purpose, not for files to be put to by arbitrary programs.,In contrast, /tmp is exactly made for this. On my systems, /tmp is a tmpfs as well, in contrast to /var/tmp which is designed for putting larger files, potentially staying longer.,My testing shows little if any performance difference by putting said files in /tmp (to disk) or into /dev/shm (filesystem-level shared memory) on Linux even under moderate load. I attribute this to the filesystem cache. , 2 @glglgl - On the systems into which we're deploying (in a managed services scenario) /tmp is backed not by tmpfs but by the primary sda1 disk. Otherwise, I'd agree to keep with standard, but that's our deployment environment at this time. – Jé Queue Mar 17 '12 at 17:50

/dev/shm is intended for a very special purpose, not for files to be put to by arbitrary programs.

/dev/shm
load more v
88%

/dev/shm is a temporary file storage filesystem, i.e., tmpfs, that uses RAM for the backing store.  It can function as a shared memory implementation that facilitates IPC. ,Use /tmp/ for temporary files. Use /dev/shm/ when you want shared memory (ie, interprocess communication through files).,Connect and share knowledge within a single location that is structured and easy to search.,/dev/shm is used for shared virtual memory system specific device drivers and programs.

In descending order of tmpfs likelyhood:

┌───────────┬──────────────┬────────────────┐│
/dev/shm│
always tmpfs│ Linux specific│├───────────┼──────────────┼────────────────┤│ / tmp│ can be tmpfs│ FHS 1.0│├───────────┼──────────────┼────────────────┤│ /
   var / tmp│ never tmpfs│ FHS 1.0│└───────────┴──────────────┴────────────────┘
72%

Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.,Thanks for contributing an answer to Unix & Linux Stack Exchange!, What is the difference between git add * and git add .? , Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

/dev/shm is intended to be used for shared memory segments that live in the file system. There are two types of shared memory: SysV shared memory and POSIX shared memory. POSIX shared memory uses named segments created via shm_open, and these typically live in the file system under /dev/shm, which is usually a tmpfs. /dev/shm is usually mounted nosuid and noexec.

/dev/shm

/dev/shm is intended to be used for shared memory segments that live in the file system. There are two types of shared memory: SysV shared memory and POSIX shared memory. POSIX shared memory uses named segments created via shm_open, and these typically live in the file system under /dev/shm, which is usually a tmpfs. /dev/shm is usually mounted nosuid and noexec.

shm_open

/dev/shm is intended to be used for shared memory segments that live in the file system. There are two types of shared memory: SysV shared memory and POSIX shared memory. POSIX shared memory uses named segments created via shm_open, and these typically live in the file system under /dev/shm, which is usually a tmpfs. /dev/shm is usually mounted nosuid and noexec.

/dev/shm

/dev/shm is intended to be used for shared memory segments that live in the file system. There are two types of shared memory: SysV shared memory and POSIX shared memory. POSIX shared memory uses named segments created via shm_open, and these typically live in the file system under /dev/shm, which is usually a tmpfs. /dev/shm is usually mounted nosuid and noexec.

/dev/shm

/dev/shm is intended to be used for shared memory segments that live in the file system. There are two types of shared memory: SysV shared memory and POSIX shared memory. POSIX shared memory uses named segments created via shm_open, and these typically live in the file system under /dev/shm, which is usually a tmpfs. /dev/shm is usually mounted nosuid and noexec.

nosuid

/dev/shm is intended to be used for shared memory segments that live in the file system. There are two types of shared memory: SysV shared memory and POSIX shared memory. POSIX shared memory uses named segments created via shm_open, and these typically live in the file system under /dev/shm, which is usually a tmpfs. /dev/shm is usually mounted nosuid and noexec.

noexec
load more v
65%

/dev/shm is a temporary file storage filesystem, i.e., tmpfs, that uses RAM for the backing store.  It can function as a shared memory implementation that facilitates IPC.,/tmp is the location for temporary files as defined in the Filesystem Hierarchy Standard, which is followed by almost all Unix and Linux distributions.,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.,Remember that /tmp can be part of the / filesystem instead of a separate mount, and hence can grow as required. The size of /dev/shm is limited by excess RAM on the system, and hence you're more likely to run out of space on this filesystem.

When should I use /dev/shm/ and when should I use /tmp/? Can I always rely on them both being there on Unices?

/dev/shm /

When should I use /dev/shm/ and when should I use /tmp/? Can I always rely on them both being there on Unices?

/tmp/
load more v
75%

One often has to use temporary files to store temporary data, and this is often done under UNIX using the /tmp directory (or /var/tmp directory). Well, it works, but the /tmp directory is a real directory on your hard drive (at least on Linux), and if you really need to use temporary stuff, it’s not really interesting to put it on your hard drive. Nowadays we have a lot of RAM memory. Why not using it for temporary files? This is where /dev/shm appears!,/dev/shm is a “virtual directory” that exists only in your RAM. Technically, it’s a normal directory but where part of your RAM is mounted, using a special filesystem known as tmpfs. This can be seen using mount:, Have you already noticed while listening to multi-channel music with only two stereo speakers with mplayer, how the sound is low? Same thing applies with videos using the AC3 format, often composed... , Let’s say you have a .png file, a .txt file and an .mp3 file. If you are using Konqueror (or Nautilus under Gnome), just double click, the program you’ve chosen for this type of file will be launch...

1
2
load more v
40%

tmpfs : Tmpfs is a file system which keeps all files in virtual memory.,This should read: tmpfs : Tmpfs is a file system which keeps all files in physical memory.,“tmpfs : Tmpfs is a file system which keeps all files in virtual memory.”,How to set up Redis sentinel cluster on Ubuntu or Debian Linux

If you type the mount command you will see /dev/shm as a tempfs file system. Therefore, it is a file system, which keeps all files in virtual memory. Everything in tmpfs is temporary in the sense that no files will be created on your hard drive. If you unmount a tmpfs instance, everything stored therein is lost. By default almost all Linux distros configured to use /dev/shm:
$ df -h
Sample outputs:

Filesystem Size Used Avail Use % Mounted on /
   dev / mapper / wks01 - root
444 G 70 G 351 G 17 % /
tmpfs 3.9 G 0 3.9 G 0 % /lib/init / rw
udev 3.9 G 332 K 3.9 G 1 % /dev
tmpfs 3.9 G 168 K 3.9 G 1 % /dev/shm /
   dev / sda1 228 M 32 M 184 M 15 % /boot
none / dev / shm tmpfs defaults, size = 8 G 0 0
load more v
22%

tmpfs is a temporary file storage paradigm implemented in many Unix-like operating systems. It is intended to appear as a mounted file system, but data is stored in volatile memory instead of a persistent storage device. A similar construction is a RAM disk, which appears as a virtual disk drive and hosts a disk file system. ,Extended file attributes,And on almost all Linux distributions, a tmpfs is mounted on /run/ or /var/run/ to store temporary run-time files such as PID files and Unix domain sockets. ,Comparison of file systems distributed

Many Unix distributions enable and use tmpfs by default for the /tmp branch of the file system or for shared memory. This can be observed with df as in this example:

Filesystem Size Used Avail Use % Mounted on
tmpfs 256 M 688 K 256 M 1 % /tmp
load more v