How to make RAM disks in OS X

In the classic Mac OS, one of the neat features was the ability to create a RAM disk, which would allow users to allocate a section of RAM for use as a storage drive. Being stored in RAM, the drive's contents would load nearly instantaneously and could therefore be used to quickly launch applications and load documents that were frequently accessed.

In the classic Mac OS, one of the neat features was the ability to create a RAM disk, which would allow users to allocate a section of RAM for use as a storage drive. Being stored in RAM, the drive's contents would load nearly instantaneously and could therefore be used to quickly launch applications and load documents that were frequently accessed. The RAM disk was kept active even when the computer was off, so in some cases if you had enough RAM you could even copy the system folder to it and load the Mac from a cold boot in a few seconds.

In OS X Apple has done away with RAM disk management and creation in favor of tools like disk images and networked drives; however, RAM disks can still be created if you want to use one.

Creating the disk

Buried in the manual pages for Apple's command-line disk-image-management utility "hdid" are a few instructions on how to use this command to create and mount a RAM disk. The process basically involves creating a mount point for the disk, using the hdid command to allocate the RAM for the disk, and then create a file system on the disk so it can be used and mount it to the mount point.

#!/bin/sh

NUMSECTORS=128000
mydev=`hdid -nomount ram://$NUMSECTORS`
newfs_hfs $mydev
mkdir /tmp/mymount
mount -t hfs $mydev /tmp/mymount

Copy this text to a plain text document (not rich text) in TextEdit, and save it with ".sh" as the filename suffix (i.e., "ramdisk.sh"). Then open the Terminal and type "chmod u+x " followed by a single space, drag the script file to the Terminal window, and press enter. This will enable execution of the script by the owner of the file (you).

After this is done, to run the script just open a Terminal window, drag the script to it, and press enter. The default value for "NUMSECTORS" in the script will give you a 64MB RAM disk, but you can increase this number if you would like. The number represents sectors, and there are 512 bytes per sector of RAM. To edit the script after it has the .sh filename suffix, right-click it and choose TextEdit under the "Open With..." menu.

You can also run a similar script in one line using Apple's "diskutil" and "hdiutil" commands, which result in a more user-friendly RAM disk than the previous script.

diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://128000`

When run in the Terminal, this command does a number of things (note the single quotes are actually "grave accent" marks). First it invokes "hdiutil" to attach an allotment of RAM but not yet mount it since there is no filesystem on it yet. After the RAM is allotted, it calls on the "diskutil" command to format the allotted RAM and name it "ramdisk," which will cause the Finder to mount it under that name in the default mount point /Volumes/.

This may be a preferred method because it is simpler and allows you to name the disk by changing the "ramdisk" name in the Finder.

Limitations and practicality

There are some limits like not being able to rename the RAM disk, but files copied to it should load very fast. For instance, on my Mac Book Pro if I create a RAM disk and copy Firefox to it, loading the program shows only a single bounce of its icon in the Dock, whereas loading it from the hard drive results in 3 to 4 bounces (keep in mind it is still loading plug-ins and preferences from disk even when the application is on the RAM disk).

This may be practical for some uses, and likely impractical for most, but has been a fun feature of the Mac OS and it's good to see it is still around. While you can allocate memory for use as a drive, there may be times under heavy RAM where memory paging may result in part or all of the RAM disk getting written to the hard drive as virtual memory, defeating the purpose of having the RAM disk. In OS 9 and earlier you could turn off virtual memory (at the higher risk of memory fragmentation) to ensure the RAM disk would stay in RAM, but this option is not available in OS X.

The last thing to keep in mind about RAM disks is that in OS X they are not persistent. If you reboot the system, the RAM disk will disappear along with everything on it, so use it only as a temporary (but potentially fast) storage space during the current boot session.

Alternatives

While RAM disks were a practical way to create a fast storage location, current technologies may be a lot more convenient options. While they are still expensive, SSD drives are significantly faster than mechanical hard drives and can be used to make the whole storage drive similar to a RAM drive. Granted RAM itself will always be faster than storage media, but for most intents an SSD drive can serve the same purpose as a RAM disk.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Show Comments Hide Comments