RAM Disk on MacOS

Roland RobeRoland Robe
5 min read

Creating a RAM Disk on Mac OS is easy and will speed up some I/O-intensive work and protect your SSD. It can be created with a bash one-liner. You will see a copy-paste-ready example below.

💥
CAUTION: The code on this page is using diskutil, which when improperly used, can result in data loss. Most of the diskutil commands do not present a confirmation prompt. To avoid any damage, I will explain step by step what it is doing and how it is avoiding the risk.

How it works

Creating a RAM-Disk is the act of creating/attaching a virtual disk image and erasing it. So we need two commands. hdiutil to create/attach the device and diskutil to erase it.

The method to call it in one command almost eliminates the risk of accidentally picking and erasing the wrong volume as the RAM disk is created in the same command as it is erased.

To attach a memory device you use the command hdiutil attach ram://<size>. It is only usable after erasing the volume with the
diskutil erasevolume <file-system-format> <volume-name> <device> command.

To pass the device as a set of commands, you need to put it in back-ticks. Putting it all together looks like this: (The backslash is to break the line for better readability)

diskutil erasevolume <file-system-format> <volume-name> \
`hdiutil attach -nomount ram://<size>`

Example: Creating a 265MB RAM Disk

To create a RAM-Disk called myRAMdisk with the size of 256 MB enter the command like below:

diskutil erasevolume HFS+ "myRAMdisk" \
`hdiutil attach -nomount ram://524288`

This command will create the RAM Disk and mount it, so it is ready to use and it shows up in Finder as well.

Explaining it step by step

The command is two commands in one. Let's break them down.

The second command is the <device> parameter of the first command.
diskutil <command-verb> <format> <name> <device>

  1. diskutil erasevolume HFS+ "myRAMdisk":

    • diskutil: is the command-line utility in macOS for managing disks and storage devices.

    • erasevolume: This subcommand of diskutil is used to erase a volume and format it with a specified file system.

    • HFS+: Specify the file system format you want to use, in this case, HFS+ (Mac OS Extended). Other formats are possible, depending on the macOS version you run (APFS, HFS+, FAT32, exFAT, MS-DOS)

    • "myRAMdisk": This is the name you are giving to the newly created RAM disk. It will be formatted with the HFS+ file system and named "myRAMdisk."

  2. hdiutil attach -nomount ram://524288: (passed as <device> into diskutil)

    • hdiutil: This command is used for managing disk images in macOS. In this case, it's being used to create the RAM disk.

    • attach: This subcommand tells hdiutil to attach or create a disk image.

    • -nomount: This option specifies that the RAM disk should be attached but not automatically mounted. In other words, the RAM disk will be recognized by the system but won't appear on the desktop or in Finder. This is required as we pass it as the <device> parameter

    • ram://524288: This part of the command specifies the source for the RAM disk. ram:// is a special scheme used by hdiutil to create a RAM-based disk. 524288 is the size of the RAM disk in 512-byte blocks, which in this case is 256 megabytes (MB) since 1 block is 512 bytes.

Calculating the size

The size attribute is written in "number of blocks of 512-bytes". This means you need to multiply the desired size in MB by 2048. To create a 256MB Drive you need to use the value 524'288. The desired size doesn't need to match a multiply by eight. You can approximate the size as you need, as the number represents the count of blocks you reserve.

Example values:

Desired Size in MBCalculationValue to useComment
256256 * 2048524'288256 Mega Byte
10241024 * 20482'097'1521 Giga Byte
10001000 * 20482'048'000~ 1 Giga Byte

Why is the factor 2048?

Lets take again the example of the 256 MB Drive

256 MB (Mega Byte) * 1024 => 262'144 KB (Kilo Byte)
262'144 KB (Kilo Byte)* 1024 => 268'435'456 Byte
268'435'456 Byte / 512 byte block-size => 524'288 (number of blocks)

Therefore the calculating factor is 1024 * 1024 / 512 = 2048

Dropping the RAM Disk

To delete the disk, you either just eject it from the Finder or call the following command. diskutil eject <disk-name>

Example:

diskutil eject "myRAMdisk"

Usage note

A RAM disk is very useful for the following cases:

  • Creating big test files. The generator of the test file is checked in GIT and the file is only created on demand and doesn't waste space on your disk and hammer your SSD.

  • Requiring fast I/O for testing/processing purposes. E.g. writing many small temporary files or modifying them within a short period over and over again.

Don't create a RAM Disk that is bigger than your free memory (which the system will allow you to do )
  • The system allocates the space only once it is used.

  • If you use more memory than you have, the system starts swapping memory to disk to free up memory. This will be counterproductive and will make the whole system slow.

NOTE: Your data on a RAM Disk are ephemeral/temporary. When you eject the drive or boot your system, your data is gone as well.

Conclusion

RAM Disks used right can be very useful.

  • Creating a RAM Disk on MacOS is easy by writing a one-liner to the command prompt.

  • Using a one-liner almost eliminates the risk of erasing the wrong volume

  • The RAM Disk can extend your SSD live, as it reduces your write cycles.

  • It is great for ephemeral/temporary data.

1
Subscribe to my newsletter

Read articles from Roland Robe directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Roland Robe
Roland Robe