What is the usecase of dev shm

/dev/shm is a temporary file storage filesystem (see tmpfs) that uses RAM for the storage.

It can function as shared memory that facilitates IPC. It is a world-writeable directory.

Its use is completely optional within the kernel config file (i.e. it is possible not to have dev/shm at all)

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. (That said, /tmp sometimes use RAM storage too)

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.

Use-cases

  1. To minimize disk I/O. For example, I need to download very large zip files from an FTP server, unzip them, and then import them into a database. I unzip to /dev/shm so that for both the unzip and the import operations the HD only needs to perform half the operation, rather than moving back and forth between source and destination. It speeds up the process immensely.
  2. /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.
  3. You can use /dev/shm to improve the performance of application software such as Oracle 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).

In this example, remount /dev/shm with 8G size as follows:


# mount -o remount,size=8G /dev/shm

How to keep /dev/shm permanently?

You need to add or modify entry in /etc/fstab file so that system can read it after the reboot. Edit, /etc/fstab as a root user, enter:


# vi /etc/fstab


Append or modify /dev/shm entry as follows to set size to 8G

none      /dev/shm        tmpfs   defaults,size=8G        0 0

Save and close the file.

For the changes to take effect immediately remount /dev/shm:


# mount -o remount /dev/shm


Verify the same:


# df -h

Resources