This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.
Last Updated: 2024-11-23
/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.
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.
/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.