Putting chromes cache in RAM

May 24, 2010, tags: bash and tips

UPDATE: This was written many years ago may not be useful today The Chrome web browser is changing the way we browse with it’s speed and focus on content. But, it’s killing my laptop’s battery and hard drive with its constant reading and writing to disk.

logo

Luckily, Linux has a folder mounted in the ram, which means that reads and writes to this folder don’t affect the harddisk (it’s also about two orders of magnitude faster than my notebook disk). The downside is that the data disappears when the power goes out. That folder is most often called /dev/shm/ and I wanted to put Chrome’s cache in this folder, to avoid the constant writing to disk. But I wasn’t prepared to lose all my preferences, extensions, and saved data when the computer was powered down. So I wrote this script that copies the folder from /dev/shm/ mounted in the ram to the disk and this script runs whenever I exit Chrome.

This is how I put Chrome’s cache in the ram

mkdir /dev/shm/.personal_synced # The folder in ram

mkdir ~/.personal_synced # The mirrored folder on disk

I wrote this simple script that fires once every hour (I’m pretty sure it can be written in a simpler way, but I’m certainly a bash-newbie):

#!/bin/bash
# Sync the ramdisk to harddrive

foldername=”.personal_synced”
ramdisk=”/dev/shm/”$foldername
harddisk_copy=”/home/anders/”
backup=”/home/anders/”$foldername”.old”

# Remove the old backup
echo “removing $backup”
rm -fr “$backup# Make a copy of the old one
echo “Copies the latest backup $harddisk_copy$foldername”
mv -f “$harddisk_copy$foldername” “$backup# Copy from ram
echo “Copies $ramdisk to harddrive, $harddisk_copy”
cp -R “$ramdisk” “$harddisk_copy

I want to make this sync-script fire when I exit Chrome. At first I tried to make it fire on shutdown but that proved harder than I thought. The following script synchronizes the ram-disk when Chrome exits, and I just replace the symbolic link in /usr/bin/google-chrome with this script.

#!/bin/bash
# Start Chrome with cache in the ram

/opt/google/chrome/google-chrome -user-data-dir=”/dev/shm/.personal_synced/google-chrome” “$@&& /home/anders/Scripts/syncRamdisk

All that is missing is a script that copies the disk-folder to ram when we boot our computer.

# The synced folder
cp -R /home/anders/.personal_synced /dev/shm/
chown -R anders /dev/shm/.personal_synced

# The chrome cache. I don’t want to sync this so it’s placed outside .personal_synced
mkdir /dev/shm/google-chrome
chown anders /dev/shm/google-chrome

I simply added the script to my Startup Applications, under System->Preferences.

Moving Chrome’s cache to our automatically synced folder

By moving the .config/google-chrome-folder to the synced folder, and replacing it with a symlinks, the work will be complete (I also move the cache to the ramdisk, but not to the synced folder. I see no point in spending time and space on keeping the cache):

mv ~/.cache/google-chrome /dev/shm/google-chrome
ln -s /dev/shm/google-chrome ~/.cache/google-chrome

mv ~/.config/google-chrome /dev/shm/.personal_synced/
ln -s /dev/shm/.personal_synced/google-chrome ~/.config/google-chrome

Notes:

/dev/shm/ is readable by anyone, it’s not very secure to put stuff in that folder.

Update: I adjusted the scripts, I had problems with root owning the folders which caused some problems.