Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers

up vote 22 down vote accepted

/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.

http://en.wikipedia.org/wiki/Shared%5Fmemory:

Recent 2.6 Linux kernel builds have started to offer /dev/shm as shared memory in the form of a ramdisk, more specifically as a world-writable directory that is stored in memory with a defined limit in /etc/default/tmpfs. /dev/shm support is completely optional within the kernel config file. It is included by default in both Fedora and Ubuntu distributions, where it is most extensively used by the Pulseaudio application.

/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.

To answer your questions: No, you cannot always rely on /dev/shm being present, certainly not on machines strapped for memory. You should use /tmp unless you have a very good reason for using /dev/shm.

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.

share|improve this answer
    
I will be using it to redirect output from a commands' standard error output to a file. Then I will read this file and process it. I will be doing this several thousand times (it's part of the condition of a loop construct). I thought memory would be nice in this case. But I also want it to be portable. I guess I'll check if /dev/shm exists, use it if it does, or fallback to /tmp. Does that sound good? –  Deleted Sep 23 '09 at 16:04
1  
I'd also add a check for the minimum size and current usage level of /dev/shm to guard against inadvertently filling it up. –  nagul Sep 23 '09 at 21:01
2  
Under Linux 2.6 and later the /dev/shm is required to be mounted for the POSIX shared memory system calls like shm_open() to work. In other words some programs will break if its not mounted - so it should be. It is not just a RAM disk. So you should make sure some of /dev/shm is free. –  EdH Nov 30 '12 at 21:07
1  
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. –  Gilles Jun 18 '13 at 7:13
2  
@GaretClaborn There's plenty of good reasons to use memory backed by swap, but that's called normal process memory. If you're using a file, it's called a filesystem, and all filesystems are memory (cache), which is backed by swap if the filesystem is something like tmpfs. Allocating disk space between swap and other storage areas is typically in the real of the administrator. If an application wants files that tend to remain in RAM, /tmp is the normal location (with $TMPDIR to override). The choice to make /tmp backed by swap, other disk space or nothing is the administrator's. –  Gilles Jun 5 at 14:09
show 3 more comments

Okay, here's the reality.

Both tmpfs and a normal filesystem are a memory cache over disk.

The tmpfs uses memory and swapspace as it's backing store a filesystem uses a specific area of disk, neither is limited in the size the filesystem can be, it is quite possible to have a 200GB tmpfs on a machine with less than a GB of ram if you have enough swapspace.

The difference is in when data is written to the disk. For a tmpfs the data is written ONLY when memory gets too full or the data unlikely to be used soon. OTOH most normal Linux filesystems are designed to always have a more or less consistent set of data on the disk so if the user pulls the plug they don't lose everything.

Personally, I'm used to having operating systems that don't crash and UPS systems (eg: laptop batteries) so I think the ext2/3 filesystems are too paranoid with their 5-10 second checkpoint interval. The ext4 filesystem is better with a 10 minute checkpoint, except it treats user data as second class and doesn't protect it. (ext3 is the same but you don't notice it because of the 5 second checkpoint)

This frequent checkpointing means that unnecessary data is being continually written to disk, even for /tmp.

So the result is you need to create swap space as big as you need your /tmp to be (even if you have to create a swapfile) and use that space to mount a tmpfs of the required size onto /tmp.

NEVER use /dev/shm.

Unless, you're using it for very small (probably mmap'd) IPC files and you are sure that it exists (it's not a standard) and the machine has more than enough memory + swap available.

share|improve this answer
add comment

Use /tmp/ for temporary files. Use /dev/shm/ when you want shared memory (ie, interprocess communication through files).

You can rely on /tmp/ being there, but /dev/shm/ is a relatively recent Linux only thing.

share|improve this answer
    
Isn't there a performance aspect as well? As /dev/shm is most often mounted as a tmpfs volume and essentially a RAM-disk? –  Deleted Sep 23 '09 at 9:37
    
You can also mount /tmp as a tmpfs filesystem, I do so on my netbook to speed some things up by reducing writes to the (slow) SSD. There are disadvantages to doing so, of course (mainly the RAM use, but my netbook has far more RAM than it generally needs anyway). –  David Spillett Sep 23 '09 at 10:55
    
For my specific case I would use it for a sort of process communication. I capture the output of standard error from an application and act on the contents (and I still need the standard output untouched, so I can't do any 1>/dev/null 2>&1. I would do this several thousand times so a tmpfs would be nice. However if I release the script I can't rely on tmpfs being used for /tmp as I think it's not that common. If it's more common for /dev/shm then that's better for me. But I'm looking for guidelines regarding portability etc. –  Deleted Sep 23 '09 at 16:00
add comment

/dev/shm is used for shared virtual memory system specific device drivers and programs.

If you are creating a program that requires a virtual memory heap that should be mapped to virtual memory. This goes double so if you need multiple processes or threads to be able to safely access that memory.

The fact is that just because the driver uses a special version of tmpfs for it, doesn't mean you should use it as a generic tmpfs partition. Instead, you should just create another tmpfs partition if you want one for your temporary directory.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.