tmpfs and /dev/shm

What is tmpfs?

tmpfs is a temporary filesystem which lives completely in the memory's page cache and on swap.

what is /dev/shm?
  1. It's an instance of tmpfs.
  2. There're other tmpfs instances like /run, /tmp base on different Linux distributions.
  3. The default size of /dev/shm is half of your memory size. But this size is only the maximum amount of memory it can use. It doesn't actually use that memory unless its storing something.
Why put files into tmpfs?
  1. Speed up accesses to these files
  2. Efficiently passing data between programs(for shared memory)
  3. Ensure these files will automatically cleared upon reboot. (e.g., .pid and lock file).
    *Note: When using systemd, temporary files in tmpfs directories can be recreated at boot by using tmpfiles.d.
Practice
  • Most systems use very little shared memory, run ipcs -m to see how much yours is currently using
  • Even less actually use POSIX shared memory (/dev/shm)
  • You can mount /dev/shm with a larger size and put DB files inside for a better performance
vi /etc/fstab  
tmpfs   /tmp   tmpfs   nodev,noexec,nosuid,size=20G   0 0  
mount -o remount /dev/shm  
  • Compile source codes in tmpfs will be much faster since the entire untar and compile require no actual disk I/O

Reference
Arch Wiki - tmpfs Linux Kernel Doc