Using system memory for storage is something of a lost art these days. Although system RAM capacity has become plentiful, cheap and quick disk storage is just as common. But many of today’s I/O intensive tasks can still benefit from the untouchable quickness provided by a ramdisk. Happily, most operating systems are still capable of creating and using ramdisks. This article discusses the creation, use, and performance of ramdisks in Mac OS X.
On The Extinction Of Ramdisks
Ramdisks were a cheat to make slow system I/O tolerable, but it was never an easy decision. These days, computers are fast enough that ramdisks are rare. Every part of the computer has gotten quicker and cheaper over the last two decades, including hard disk drives, RAM, and CPUs. A typical laptop like my MacBook Pro might have 4 GB of system RAM, 320 GB of hard disk capacity, and dual 2 GHz CPUs.Those specs would have seemed inconceivable just a few years ago: I still have a Dell laptop with a 20 MHz CPU and two expensive upgrades: 1 MB of RAM and a 20 MB hard drive!
Far more impressive, though, are the numerous performance optimizations that have appeared. I/O channels are quicker than ever, and caches have appeared at most performance bottlenecks. It’s hard to believe, but computers used to rely much more on raw storage. Many hobbyists recall waiting for data to load from cassette tape drives, or that one key performance advance in Intel’s 80486 chip was its on-chip cache. Today’s CPUs have three levels of cache, and operating systems have gotten much better at caching data internally as well.
Hard disk drives have sprouted their own caches, too, making I/O seem quicker than it is. My 1997 Toshiba laptop used an un-cached 4200 rpm hard disk, but even laptop drives today sport 16 MB or more and spin at 5400 rpm or faster. Although RAM was scarce back in the day, the performance gained from a ramdisk was worth the effort. Faster I/O and strategic caching has largely eliminated this need. Increasing operating system intelligence also plays a major part – witness the failure of hybrid hard drives.
The Modern Use Case
A long-standing argument in the field of computer system performance revolves around whether to manually reserve resources and place data or to let the system dynamically manage resources on its own. Computers do a decent job if allowed to, often adapting quicker and more efficiently to changing demands. But some jobs are harder to automate, and buffers and caches don’t always catch the right data.
Purely temporary data can be written to a ramdisk as a high performance scratch space. There is no need to store this on disk at all, and merely placing it there is likely to “pollute” the buffers, pushing out real useful data.
A ramdisk can also increase security somewhat by never allowing certain data to be written to disk. Ramdisks are sure to be flushed on the next reboot, but data on disk can linger and be discovered later.
One could even argue that today’s high-performance solid state disk is simply an evolution of the old ramdisk concept. NAND flash and RAM both offer the high random performance of the ramdisks of yore, and many analysts expect them to displace high-performance hard disk drives in the coming years. So perhaps ramdisks are not extinct after all!
Using Ramdisks In Mac OS X
Mac OS X has the ability natively to create and use ramdisks. Here’s the simple procedure:
- Create the ramdisk with hdiutil – This terminal command will create a 1 GB ram disk and report the name used. The number following the “ram://” statement is a number of 512 KB disk blocks – multiply the desired ram size in MB by 2048 (that’s 1024 times 2) to derive other sizes.
hdiutil attach -nomount ram://2097152
- Format the ramdisk with diskutil – The next command will format the newly-created ramdisk using the diskutil tool. We will use /dev/disk3, but you should specify whatever disk device hdiutil reported from step 1. You can also specify a different filesystem or disk name.
diskutil eraseVolume "HFS+" "ramdisk" /dev/disk3
- Your ramdisk will now appear – OS X will now mount the newly-created ramdisk and it will appear on your desktop. Note that the contents will be destroyed if it is unmounted or if the operating system is rebooted!
- Delete your ramdisk – It’s worth saying once again: The ramdisk will disappear and all data will be deleted when you eject it or power off your computer. Be sure to back up any important data from it, then eject it with the following command:
hdiutil eject /Volumes/ramdisk
My backup script called for the creation of a tar file, compression, encryption, and rsync copying. I decided that I could perform the first three functions on the ramdisk to increase performance and reduce the chance that an unencrypted version would remain on the disk if the script failed.
A simple command line combination of the above creates the necessary ramdisk:
diskutil eraseVolume "HFS+" "ramdisk" `hdiutil attach -nomount ram://2097152`
This failed once I put it in a shell script, however. The issue was the finicky nature of diskutil – hdiutil’s output included a number of whitespace characters, and diskutil failed in the script whenever it encountered this. So I used the cut command to grab just the first field of the output, like so:
diskutil eraseVolume "HFS+" "ramdisk" `hdiutil attach -nomount ram://2097152 | cut -d " " -f 1`
This solved my problem, and I was able to tar and compress to /Volumes/ramdisk in my script. I added the hdiutil eject command to the script right after the encryption step to wipe out the unencrypted data. Although this isn’t foolproof security by any means, it’s better than nothing.