Linux /dev/shm/: a Shared Memory File Directory

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

/dev/shm is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device.

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.

How do I restrict or modify size of /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. Then append or modify /dev/shm entry as follows to set size to 2G:

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

Save and close the file. For the changes to take effect immediately remount /dev/shm:

mount -o remount /dev/shm

Verify:

df -h