OS X Ramdisk tutorial


This year I got my MacBook Air 13" for Christmas. The packet arrived just in time on 23rd of December - what a relief!
Since the Air comes with an ssd, which has in my case the capacity of 128 Gb, and since I'm - let's say - sometimes a bit too cautious regarding SoC features (like the ssd) that can not be replaced by myself, I decided to set up a ramdisk on my Air. In this case, it's not for the purpose of speed but rather to avoid unnecessary write cycles, which cause attrition.
To do so, I wrote a bash-script and saved it as "/etc/rc.local". This way it will be executed on startup. The following command creates a ramdisk with the size of 2048 Mb. Since you have to multiply your desired size in Mb by 2048 to get the real number for the command, my final size is 4194304.
> diskutil erasevolume HFS+ "RamDisk" `hidutil attach -nomount ram://4194304`
At first the memory will be allocated but not yet mounted. Then it will be passed to the diskutil command which mounts and names the Volume in a very convenient way. "RamDisk" is in this case the name of the mounted Volume. Normally you need to be a root user to use this command. Although if you are using the script, mentioned earlier, this is not necessary, 'cause it already has root privileges.
After executing this command a little icon appeared on my desktop, displaying my ramdisk. Even after quite a short time, this icon started to bug me. Because I did not want to ban all volume icons or icons in general from my desktop, I decided to add the following line, which enables me to hide only my ramdisk volume icon from the desktop.
> chflags hidden "/Volumes/RamDisk"
If you try this command, you will notice that the icon will still be visible on the desktop. The reason for this is that you have to restart the Finder in order to make things happen. What's nice about the script, executed on startup, is that the "chflags"-command is called before the Finder launches. Therefore you do NOT have to restart the Finder again.

Using the Ramdisk for Browser Cache:

My aspiration is to use the ramdisk for browser cache. Since my cache gets very big from time to time, I would like to spare my ssd from unnecessary write cycles. Therefore I created a cache folder for every browser, I'm mainly using. After that those folders were soft-linked to the standard locations of the cache folders.

The cache folder for Safari is located here:
~/Library/Caches/com.apple.Safari
And the cache for SRWare's Iron1 is located in:
~/Library/Caches/Chromium/Default/Cache

Further Information:

I advice you not to use a ramdisk with a size of over 50 % of your available ram, in order to avoid a lack of ram for the active apps. This should only be a guideline - if you are an advanced user or you know what you are doing, you can of course set the size of the ramdisk as you like. Furthermore you may want to add some other browser's cache to the ramdisk. All you need to do is: Find the specific folder and link it to a corresponding folder on the ramdisk. That's all! ;-)

I hope my post could be of some help for you guys. If you have any suggestions for improvement or any kind of feedback, you are welcome to share what's on your mind in the comment section below.

Downloads

File: /etc/rc.local
#!/bin/sh if [ ! -d "/Volumes/RamDisk" ]; then diskutil erasevolume HFS+ "RamDisk" `hdiutil attach -nomount ram://4194304` echo "create RamDisk" chflags hidden "/Volumes/RamDisk" # killall Finder # You dont have to do this, as mentioned above. fi if [ -d "/Volumes/RamDisk" ]; then if [ ! -d "/Volumes/RamDisk/com.apple.Safari" ]; then rm -rf ~/Library/Caches/com.apple.Safari mkdir /Volumes/RamDisk/com.apple.Safari ln -s /Volumes/RamDisk/com.apple.Safari/ ~/Library/Caches/com.apple.Safari fi if [ ! -d "/Volumes/RamDisk/Chromium/Default/Cache" ]; then rm -rf ~/Library/Caches/Chromium/Default/Cache mkdir -p /Volumes/RamDisk/Chromium/Default/Cache ln -s /Volumes/RamDisk/Chromium/Default/Cache/ ~/Library/Caches/Chromium/Default/Cache fi if [ -d "/Volumes/RamDisk/com.apple.Safari" ] && [ -d "/Volumes/RamDisk/Chromium/Default/Cache" ]; then echo "RamDisk initialized successfully!" else echo "Error: Creating Cache Folders!" fi else echo "Error: No RamDisk found!" fi

Keine Kommentare:
Post a comment