What is /dev/shm and its practical usage

by nixcraft · 16 comments

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

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 16 comments… read them below or add one }

1 Vivek Poduval 03.04.08 at 3:01 pm

Very good Description.

I want to know the concept of /dev/shm in FreeBSD.

2 Scott Marlowe 05.05.08 at 3:09 pm

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.

3 vivek 05.05.08 at 4:13 pm

I you have 32G add 8G for virtual machine. I’m not asking to allocate all ram.

HTH

4 Peter Teoh 07.06.08 at 12:44 am

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?

5 DeveloperChris 07.30.08 at 11:51 pm

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

6 pavan 09.22.08 at 7:03 am

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

7 susan 10.08.08 at 8:31 pm

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

8 Noob 10.13.08 at 2:12 pm

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 ?

9 dreamingkat 11.16.08 at 2:12 pm

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

10 Grrry 01.30.09 at 2:06 pm

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.

11 Solaris 07.12.09 at 12:08 pm

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.

12 Phil 09.27.09 at 7:07 pm

So is /dev/shm effectivly a ramdisk? but using virtual memory?

13 DeveloperChris 09.28.09 at 6:53 am

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

14 Jenny Olsson 09.28.09 at 4:58 pm

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.

15 Shinoj Mathew 10.14.09 at 5:07 am

Dear friend,
It’s really helpful and i can solve a giant oracle configuration problem easily with the above description..

thank you..

16 Massive 11.08.09 at 10:16 pm

I think It’s worth to mention to /dev/shm with nosuid,noexec options

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: