How to create a RAM disk

2006-05-09 06:03:00 -08:00

Added 2007-09-29: This is the hard way. The easy way is my free app Make RAM Disk. It does the same process shown here, but with a lot less effort from you.

I just posted to this lisppaste with a punt solution. Since this solution really has nothing to do directly with the problem stated, I thought I’d share it with you as well, with HTMLization and some editing from the original.

  1. hdiutil attach -nomount ram://num_sectors (sector = 512 bytes = 0.5 K)

    This outputs a device path (/dev/foo) on stdout.

  2. newfs_hfs -v volume_name device_path

    Formats the RAM disk as HFS+. See the manpage for other options.

    [Added 2007-09-23] If you want a case-sensitive file-system (HFS+ being case-insensitive by default), add the -s flag before the device_path.

    [Added 2007-10-14] If you’d rather not use HFS+, then you can use newfs_msdos to get FAT12, FAT16, or FAT32 (you choose which with the -F option), or newfs to get UFS.

  3. diskutil mount device_path

    The RAM disk will be mounted at /Volumes/volume_name.

You could also do something involving mount(8) instead of diskutil if you wanted to mount it inside NSTemporaryDirectory(). Remember to unmount it (hdiutil detach device_path, or the Eject command in the Finder or Dock) when you’re done.

UPDATE 2007-08-07: Changed from hdid to hdiutil attach, since hdid is deprecated and hdiutil works just as well.

UPDATE 2007-09-29: Added link to Make RAM Disk.

UPDATE 2007-10-14: Added references to other commands in the newfs family (specifically, newfs_msdos and newfs).

6 Responses to “How to create a RAM disk”

  1. Dale philpott Says:

    Hi, Is there a way to create ram disks bigger than 2gb? if I try and create anything bigger then 2gb I get an error, hdiutil exited abnormally with status 1

    I wanted to create a ram disk 4 8 or 16gb is size as I have 32gb ram

    Thanks

    Dale

  2. Peter Hosey Says:

    Dale philpott: Not using this method. The disk-images system is still 32-bit (even on Leopard), so it can’t allocate 4, 8, or 16 GiB of RAM.

  3. Rollo Carpenter Says:

    Like Dale back in Nov 07, I need a very large RAM disk, and have not found a way. Have you come across anything?

    Thanks

    Rollo

  4. Peter Hosey Says:

    Rollo: Nope. As I said above: Even on Leopard, the disk images system doesn’t support RAM disks larger than 2 GiB. I know of no other way to effect a RAM disk than through the disk images system.

    If I ever find one, I’ll post about it and link to it from this post.

  5. Rollo Carpenter Says:

    I’m glad to report that my Mac Pro’s 28Gb are not wasted after all. It takes a day or two, but after a while my databases (for the jabberwacky.com conversational AI) find their way into some ‘Active’ and more ‘Inactive’ (but still re-used) memory, such that the HD access is vastly reduced, which was the whole point. So reports of RAM disks not being necessary are indeed true, as long as you’re willing to wait a bit.

  6. Keith Says:

    Rollo, if you have performance issues due to disk but you have sufficient RAM to cache it all, one thing you can try is a script, either at startup of your application or running in the background, which reads everything into memory. I’ve been known to do the following to force the caching of a large directory:

    find /directory -type f -print0 | xargs -0 -Ifile dd if=file of=/dev/null bs=4096

    And, while I haven’t actually tried this yet (so far my RAMdisk needs don’t exceed 1GB, let alone 2GB) …

    Creating a RAID of RAMdisks should be possible. Just use level 0 or linear concatenation. With the latter you should be able to use any arbitrary number of RAMdisks of up to the 2GB size limit, to get the desired size.

    –Keith

Leave a Reply

Do not delete the second sentence.