Thursday, December 25, 2014

mac os x /private temp virtual memory

Use RAM disk or HDD for temporary files

If you have enough RAM, you can dedicate (typically around 256 to 512 MB) of RAM to a RAM disk. RAM disk is a virtual disk that only resides in memory, so is suitable for storing data that need to live only until you shut down your computer. Temporary files are ideal for this. You can create a RAM disk during the boot time and redirect all the temporary files there. To do that, create a file named “MoveTempFoldersToRamDisk.sh” in your home directory and put the following content in:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
#!/bin/bash
 
# +----------------------------------------------------------------------+
# | |
# | Set up Mac OS X to store temporary files in RAM rather than on disk.|
# | |
# | By Philipp Klaus |
# | |
# | Originally by Ricardo Gameiro |
# | Changes by Daniel Jenkins |
# | |
# | |
# +----------------------------------------------------------------------+
 
cd /System/Library/StartupItems
sudo mkdir RamFS
sudo chown -R root:wheel RamFS
sudo chmod -R u+rwX,g+rX,o+rX RamFS
cat << "EOF" | sudo tee RamFS/RamFS > /dev/null
#!/bin/sh
# Create a RAM disk with same perms as mountpoint
 
RAMDisk() {
mntpt=$1
rdsize=$(($2*1024*1024/512))
echo "Creating RamFS for $mntpt"
# Create the RAM disk.
dev=`hdik -drivekey system-image=yes -nomount ram://$rdsize`
# Successfull creation...
if [ $? -eq 0 ] ; then
# Create HFS on the RAM volume.
newfs_hfs $dev
# Store permissions from old mount point.
eval `/usr/bin/stat -s $mntpt`
# Mount the RAM disk to the target mount point.
mount -t hfs -o union -o nobrowse $dev $mntpt
# Restore permissions like they were on old volume.
chown $st_uid:$st_gid $mntpt
chmod $st_mode $mntpt
fi
}
 
# Test for arguments.
if [ -z $1 ]; then
echo "Usage: $0 [start|stop|restart] "
exit 1
fi
 
# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common
 
StartService () {
ConsoleMessage "Starting RamFS disks..."
RAMDisk /private/tmp 256
RAMDisk /var/run 64
#RAMDisk /var/db 1024
#mkdir -m 1777 /var/db/mds
}
StopService () {
ConsoleMessage "Stopping RamFS disks, nothing will be done here..."
# diskutil unmount /private/tmp /private/var/run
# diskutil unmount /private/var/run
}
 
RestartService () {
ConsoleMessage "Restarting RamFS disks, nothing will be done here..."
}
 
RunService "$1"
EOF
sudo chmod u+x,g+x,o+x RamFS/RamFS
 
 
cat << EOF | sudo tee RamFS/StartupParameters.plist > /dev/null
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>Description</key>
<string>RamFS Disks Manager</string>
<key>OrderPreference</key>
<string>Early</string>
<key>Provides</key>
<array>
<string>RamFS</string>
</array>
<key>Uses</key>
<array>
<string>Disks</string>
</array>
</dict>
</plist>
EOF
Now, run the following in the Terminal:
chmod 755 ~/MoveTempFoldersToRamDisk.sh
~/MoveTempFoldersToRamDisk.sh
This creates two RAM disks on startup – one 256MB large for /private/tmp (command “RAMDisk /private/tmp 256″ in the middle of the above script) and another one 64MB large for /var/run. You can now delete ~/MoveTempFoldersToRamDisk.sh from your computer.
For the changes to take effect, you have to restart.
If you decide to undo this tweak in the future, you can do it simply by deleting /System/Library/StartupItems/RamFS directory from your Mac. E.g. by executing the following command in the Terminal:
sudo rm -rf /System/Library/StartupItems/RamFS
Again, restart is needed for this to take effect.
There are some small drawbacks to applying this tweak:
  • After applying it it takes a few seconds (2-3 on my machine) to shut down
  • It lowers the size of RAM usable for applications
If you are bothered by the above and have HDD in your Mac as well, you can consider moving the temporary files to HDD instead of the RAM disk. The steps are similar to moving the user home directories. E.g. to move /private/tmp, execute the following in the Terminal:
sudo ditto /private/tmp /Volumes/your_hdd_name/private/tmp
sudo rm -rf /private/tmp
sudo ln -s /Volumes/your_hdd_name/private/tmp /private/tmp

No comments: