Create RAM drives with open source tools
RAM drives are a filesystem mount point mapped to RAM instead of a hardware storage device. RAM drives are very fast and have high IOPS compared to durable storage devices, which is useful for applications like interprocess communication (when network socket pipes are not usable), temporary files, and caches. Unless synced or copied to durable storage, the contents of RAM drives are ephemeral and may be lost on reboot, logout, or unmounting. We give simple examples of creating / using RAM drives on Windows, Linux, and macOS with open source tools.
Linux RAM drive
On Linux systems, RAM drives are typically built into the OS.
Mount points /dev/shm/ and /run/shm/ map
shmem
shared memory to a RAM drive.
They are also present in Windows Subsystem for Linux (WSL).
df -kh /dev/shm
free -h
# Write 1 GB to shmem
dd if=/dev/zero of=/dev/shm/blah bs=10M count=100
df -kh /dev/shm
free -hSee which one has 1GB space more used. This tells if hard drive or RAM was used.
For programs using shared memory for heavy writing operations:
- order(s) of magnitude slower
/dev/shm/operations when HDD is used versus RAM - wearing of solid state drive if
/dev/shmis pointed there
Example data bandwidths:
- SSD: 100s of MB/sec
- HDD: 10s of MB/sec
- RAM: 1000s of MB/sec
macOS RAM drive
macOS factory program diskutil can create a RAM drive via factory program hdiutil like:
diskutil partitionDisk $(hdiutil attach -nomount ram://2097152) 1 GPTFormat APFS "RAMDisk" '100%'The command line options are:
partitionDisk- partitions a disk
$(hdiutil attach -nomount ram://2097152)- creates a RAM drive
-nomount- prevents RAM drive from being automatically mounted in Finder
ram://2097152- creates a 1 GB RAM drive (512 bytes/sector * 2097152 sectors = 1 GB)
1- creates 1 partition on the RAM drive
GPTFormat- formats the partition with GPT
APFS- formats the partition with APFS
"RAMDisk"- names the partition “RAMDisk”
'100%'- uses 100% of the RAM drive for the partition
There is a shell utility to simplify the process of creating and managing RAM drives on macOS with command line options. A Swift GUI open source app for those who prefer a graphical interface is also available.
References:
Windows RAM drive
On Windows, RAM drives can be created with the open source tool RamDrive. This is a user-mode RAM drive that can be created and used without administrator privileges, unlike kernel-mode RAM drives such as ERAM.