Skip to content

Instantly share code, notes, and snippets.

@sohooo
Created June 30, 2012 13:25
Show Gist options
  • Select an option

  • Save sohooo/3023740 to your computer and use it in GitHub Desktop.

Select an option

Save sohooo/3023740 to your computer and use it in GitHub Desktop.
create RAM disk in OS X
#!/bin/bash
#
# Create a RAM Disk in Mac OS X 10.5, 10.6, 10.7 or later.
# To remove the RAM disk, simply eject it from the desktop
# as you would any other disk.
#
# http://osxdaily.com/2007/03/23/create-a-ram-disk-in-mac-os-x/
if [ -z "$1" ]; then
echo Usage: $0 SIZE_IN_MB
exit 1
fi
MB_SIZE=$1
# The `hdiutil` wants the number in form of 512K blocks.
# However, we'd like to use values in MB.
let "MB_SIZE *= 2048"
diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://${MB_SIZE}`
@samleb

samleb commented Oct 23, 2026

Copy link
Copy Markdown

Since tmpfs dropped on macOS 15 (Sequoia), you can also use it to create RAM disks, and this approach has two massive advantages over the hdiutil/diskutil-based implementations:

  • Incomparable performance: around 11 MB/s read/write on my M1 Max Mac Studio (makes sense since it's basically a native filesystem directly managed by the VM)
  • Dynamic RAM allocation: RAM isn't "reserved" ahead of time, but usage grows with the amount of data you put on the RAM disk (10 MB of data on a 10 GB RAM disk = 10 MB of RAM used)

You can easily mount a tmpfs volume with the following commands:

  • sudo mount_tmpfs -s 10G path/to/dir — mounts the volume and temporarily hides the original folder contents
  • sudo chown $(whoami):staff path/to/dir — to get write permissions back inside the volume

Same goes for unmounting:

  • sudo umount path/to/dir — heads up, all volume contents vanish instantly, original folder files reappear

To bypass these mount/unmount "hiccups", I built a utility called RAM Folder that turns any of your folders into a tmpfs volume by importing the data into it when mounting, and saves the volume's contents back to the folder when unmounting.

Turns out the transition between a standard folder and a RAM disk is so seamless that I decided to call it: RAM Folder.

It's not open-source (yet) but you can check it out on the RAM Folder product page and there's a 14-day trial free trial.
It's super performant for build caches, hosting a unit test DB, or honestly any operation that's IO-bound.

Hope you find this interesting and take the time to give the utility a spin!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment