/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.
shm / shmfs is also known as tmpfs, which is a common name for a temporary file storage facility on many Unix-like operating systems. It is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device.
If you type 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.
Nevertheless, where can I use /dev/shm?
You can use /dev/shm to improve the performance of application software or overall Linux system performance. On heavily loaded system, it can make tons of difference. For example VMware workstation/server can be optimized to improve your Linux host's performance (i.e. improve the performance of your virtual machines).
For example, if you have 8GB RAM then remount /dev/shm as follows:
# mount -o remount,size=8G /dev/shm
To be frank, if you have more than 2GB RAM + multiple Virtual machines, this hack always improves performance.
# mount -t tmpfs -o size=5G,nr_inodes=5k,mode=700 tmpfs /disk2/tmpfs
Above will give you tmpfs instance on /disk2/tmpfs which can allocate 5GB RAM/SWAP in 5K inodes and it is only accessible by root.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins

- My 10 UNIX Command Line Mistakes
- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
Facebook it - Tweet it - Print it -
We're here to help you make the most of sysadmin work. So, subscribe!


{ 24 comments… read them below or add one }
Very good Description.
I want to know the concept of /dev/shm in FreeBSD.
I cannot see where allocating an 8G ram disk on a machine with 8G of physical ram is a good thing. The OS will start swapping out the ram disk pages long before you fill it up, slowing the whole system to a crawl.
I you have 32G add 8G for virtual machine. I’m not asking to allocate all ram.
HTH
I don’t quite understand why u need the tmpfs thing – if u have that much of memory, just write direct to memory, why set up swapspace and then write to swapspace via tmpfs?
Similarly question for /dev/shm?
Is it because of 4GB limit for memory – in 32bit OS scenario?
Peter you can’t always write direct to memory, for example I have a cgi program I want to load very fast placing it in tmpfs will permit that. If I simply place the file on the disk and ‘hope’ the disk cache caches it then it may be swapped out during times of high disk activity. by placing it in shared memory it will stay put. until of course the next reboot
DC
I am a new to linux
After reading the discussion of this article i am
really confused
firstly, what is tmpfs and swap space? where they are stored?
second, if there are any difference how this will affect the performance of a system?
third, is there any method to allocate tmpfs or swap space for a particular program?
if my english is bad please forgive
/dev/shm is a good place for separate programs to communicate with each other. For example, one can implement a simple resource lock as a file in shared memory. It is fast because there is no disk access. It also frees you up from having to implement shared memory blocks yourself. You get (almost) all of the advantages of a shared memory block using stdio functions. It also means that simple bash scripts can use shared memory to communicate with each other.
As an example, I have an external piece of hardware which only one program at a time can communicate with. I want to log data from this device as often as possible, but also run an “assay” – ie. tell it to do a sequence of tasks. I have implemented this in a client/server mode where the “server” does all of the communication and the clients request actions. The data logger requests readings as often as it can and the assay controller requests the device to change states at the appropriate times. In order to avoid conflicts each client program must check a “lock file” in /dev/shm to see if the server is available. If it is, then the client writes it own process ID ($$ in bash) to the lock runs it request, and then releases the lock (don’t forget that step!) by writing 0 back to the lock so other clients know that the server is available again.
All of this _could_ be done with a “normal” file, but would be a bit slower.
Dear Susan,
Good explanation one thing more i wana add that when we install oracle in linux os if don’t have enough size in /dev/shm then we can not increase the size of sga at first stage we have to modify the size of this shm then we can alter memory_max_target parameter. thanks hope it was useful for every one.
I’m curious to know why when I executed “mount -o remount,size=8G /dev/shm” to increase the swap, the disk size of other partitions remain the same? Where is the additional 6 GB coming from if I already have 2G of /dev/shm ?
Something I see done that’s not commented on here is using this setup for single programs that use a lot of I/O during normal operation – like qmail (as long as your willing to loose mail in the queue if the server crashes or needs to reboot).
Not sure why you think making a ramdisk for swapping/paging is a good idea. Any page fault is still overhead. If you’re having heavy swapping or page faulting but have immense amounts of memory, the issue may be in the kernel parameters, not in reducing available memory to make a ramdisk.
Oracle uses large /dev/shm to improve communication between processes.
If your /dev/shm is not large enough Oracle will not install, worse will most likely
crash.
So is /dev/shm effectivly a ramdisk? but using virtual memory?
Yes and No
Yes /dev/shm is Ramdisk
No it doesn’t use virtual memory unless you run out of ram then the OS may swap to disk (virtual RAM). I don’t know enough to say for sure if this happens actually happens but it would be most likely.
If you run out of space on /dev/shm it would report an error like any other type of disk
It’s not quite the same as a ramdisk. A ramdisk is guaranteed to be in RAM, tmpfs may be swapped out. Depending what else is using RAM at the time it may or may not be faster that writing to a disk; it generally will be of course. Personally I run with very small /dev/shm as nothing I use uses it, as far as I can see.
The equivalent for the BSDs is mfs, but there’s no standard of having it in any particular location – as far as I know anyway.
Dear friend,
It’s really helpful and i can solve a giant oracle configuration problem easily with the above description..
thank you..
I think It’s worth to mention to /dev/shm with nosuid,noexec options
Hello
It is corect this permision of ramdisk ?
drwxrwxrwt 2 root root 40 2010-05-24 15:53 ramdisk
This is a corect mount ?
# mount -t tmpfs /dev/shm /media/ramdisk
# mount -o remount,size=1756M /dev/shm
Why you use this ?
nr_inodes=5k,mode=700
Thank you !
I’m not finding this very helpful.
Like almost every manpage I’ve ever seen, this documentation assumes that you already know the stuff, and need a reminder of how to use it.
I’d like to use this–my system has 3 cpu’s and 4G memory, but runs erratically–freezing for anything from a fraction of a second to over a minute–particularly bothersome when viewing video from the hard drive, but it happens even when all I am doing is browsing.
While I understand the concept, (it is, after all an old idea,) this doesn’t help me apply it to my problem–if indeed it would help.
I’m still new to doing system admin on a linux box, though I have done many years of doing so on IBM mini’s and mainframes.
I’m old enough that my memory isn’t reliable, and my hands have tremors–making the use of the command line a pain in the (&O.
I know that I have a lot of study yet to do, but like all new systems, the amount of documentation is daunting, and much of it doesn’t help very much because it assumes a great many things–like all too much documentation written by programmers, it ignores the problems of those learning how to use things, and tends to be minimalistic and cryptic until you reach a threshold of knowledge (which I have yet to acquire.)
While reminder documentation is extremely useful, more details descriptions of what programs do and how they do it seem to me to be needed.
In general I’ve found that things which are used daily are over-documented, and things used rarely are massively under-documented. And programmers writing their own documentation are extremely likely to write only the most obscure parts of the tool instructions…because they are already intimately familiar with the main functions.
This is the kind of thing that leads to the extremely useless error messages generated by Windoze (Or an old database program I used once that gave only 4-digit error codes, all of which seemed to map to “call tech support,” bad enough, but the company no longer existed….
Am I alone in this perception?
Charles wrote:
“Am I alone in this perception?”
Yes you are!
Charles — I don’t think you’re alone in that perception. However, with enough searching it seems I eventually find *somebody’s* site that gives wonderful insight for exactly what I need to know. The main thing is I don’t expect every blogger to write a follow-up to fill me in with what is assumed I know…I go and find out. Yes it takes time, but I think that’s the place everybody learning something new has to start :(… Unless you personally know an experienced Linux guru who volunteers time to mentor you.
I think as far as /dev/shm goes, the beauty of this being mounted like a normal file system is that you can write programs (and/or scripts) to use it like you would any other file system. If you have enough RAM available, it will be much more efficient than reading/writing a hard disk. If you’re not writing programs, then /dev/shm is of little consequence to you and does not do anything for you (unless you use a program which uses this). It really should be as if it does not even exist to the average user…but for a sysadmin, it’s good to be aware of it in the event you have a program (like Oracle) that is having strange problems. Then your awareness will lead you in the right direction when researching a solution.
As far as comments about using up RAM and slowing down the whole system…well you simply need to make good decisions about using it when it makes sense to use it, and not making your entire program use it. For example, you have to make the same decision about how much data you load into RAM at a time whether using /dev/shm or just by using a big buffer and passing a pointer between applications…lots of data uses lots of RAM and doesn’t matter whether it’s shm or somewhere else.
I’m speaking out my arse here, but I think mounting 8G shm on 8G RAM is perfectly fine as it’s referencing paged memory like any other program. There is a danger that if your program uses a significant amount, you can starve other programs in RAM by forcing them to swap…but if you’re in that situation, then the active program using SHM is going to use swap if you limit SHM to say, 2G or something…so either way you lose. In the end, don’t write programs that want to load 8G into RAM during runtime. Only load things into RAM if you need to use them right away. When you’re done using it, then free it.
Hello there,
no Gentoo user here? So just my addon, shm worked like charm if you do -j3 ore more compiling with gcc, it takes under Gentoo full advantage and speed up thinks greatly, while in daily use it seems to not be every used.
Put i really would not suggest to use more than half of the memory for shm, even its used by the kernel dynamically by the need, if you have something fucked up in any service running and your shm is filling up the main memory, everything goes into swap and kills the system via I/O flood ,….
Greeting from Beijing
xuedi
/dev/shm memory is not allocated until it is needed, as it would be silly to do so otherwise.
It is fairly typical to make the size == the RAM, and just pray you (almost) never actually reach that and start swapping like crazy.
/dev/shm is essentially a ramdisk, only pretending to be a file, and it might get swapped to disk when the going gets tough. You can survive a page fault or two now and then. You can’t survive constant thrashing of your swap.
Because it pretends to be a file and lives in RAM (mostly) it makes an execellent vehicle for program communication with locks etc as a “shared memory” substitute. It’s easy to use; It lives in RAM (mostly); It is fault-tolerant (hitting swap if it has to); It’s fast
My RAM died last night. be careful if you do this stuff.