RamDisk on Linux : what it is and how to use it.
Types of Memory on a computer
In the computer there is a precise hierarchy with regard to the memory, according to their speed and size.
On a computer there are basically three types of memory: the hard drive, the RAM and the processor cache. These memories can be ordered according to the characteristics above, and then, in a hypothetical pyramid, we find at the bottom the hard drive (slow but with a lot of space, in the order of terabytes) in the center there is the RAM (fast, but with little space in the order of gigabytes) and the top there is the cpu-cache (fast but extremely small, ranging from kiloByte to megabytes).
This was true before the introduction of SSD (Solid State Disk), which led to an increase in performance in the area of HDD (though obviously not yet comparable with the RAM), one of the ways to accelerate certain processes bypassing the disc, it to use the Ram Disk.
In this way you can create a virtual disk partition in ram and transfer the program (or files used by the program) that you want to speed up.
It simply will not have to perform I/O to disk, because everything is directly in RAM.
There are however, some limitations, not all programs can be "boosted" in this way.
Verify the presence of a ramdisk
So, let's see how to use a RamDisk on Linux in my examples I've used Linux Mint 13, that is compatible with Ubuntu 12.04, but you should see no differences on other linux distributions, we'll use it to speed up Firefox!.
On most Linux distributions, you can find a device called shm in /dev (/dev/shm). SHM is an acronym for shared memory, and is the system by which programs can exchange data when they are running (for the uninitiated, a program in RAM can not access the memory partition of another program ). In particular, /dev/shm is seen by the system as a real file system that is mountable and usable as any real partition that you have on your hard disk.
To verify the partition available on your Linux you can use the command:
#df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 47G 7.5G 38G 17% / udev 3.9G 4.0K 3.9G 1% /dev tmpfs 1.6G 1.1M 1.6G 1% /run none 5.0M 0 5.0M 0% /run/lock none 3.9G 48M 3.9G 2% /run/shm /dev/sda6 416G 12G 403G 3% /home
In fact, it uses the file system type tmpfs, which is the system by which many Unix-like systems handle the storage of temporary files. All operations on the file system type occur in ram, and so when you turn off the computer the partition is removed and the files within it are simply lost.
To verify the type of file systems of the different partitions you can use the command:
/dev/sda1 on / type btrfs (rw,subvol=@)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/sda6 on /home type btrfs (rw,subvol=@home)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
So you can see that everything under /run uses tmpfs
We can then perform various operations: we can create a link to shm or use it directly, we can re-mount shm to vary the size or we can create a new partition as tmpfs file system, mount it where we want and maybe put it all in fstab to do this automatically at system startup.
How to use the Shared memory
A simple way to use shared memory with your programs is to use a link, the only thing you have to do is create a link to the shared memory device in a folder of our choice (for example, /tmp/memory ) with the command:
ln -s /run/shm /tmp/memory
Now you can just move your files there and they will be placed directly in memory. Pls Note: On Mint 13, Ubuntu 12.04 and 12.10 the device /dev/shm is a symbolic link to /run/shm . Also note that /run/shm has permissions set to 777 so everyone can read and write file there.
As general rule is better to use less than half the actual size of the RAM, to prevent the exhausting of memory for system and so going in disk swapping (a technical term that indicates when a portion of the RAM is transfered to the hard disk to free up space for the benefit of other programs or for the operating system). But if you want to do thsi change, you must put your hand on the /etc/fstab file (which controls the auto mounting of the partitions on boot). Open it with the command:
sudo vim /etc/fstab
Then modify (if it exists, better copy the line and comment the original) or add (in my case the line does not exist) the line corresponding to /run/shm:
tmpfs /run/shm tmpfs defaults,size=SIZE 0 0
substituting to SIZE the size that you want, for example to have 2 gigabytes write 2g or for 512 MB write 512m. To confirm the change you can reboot or use the command:
sudo mount -o remount /run/shm
After this command you can verify the new size with another:
If you want to resize on the fly and temporarily, simply re-mount the file system with the desired size giving this command from the terminal:
sudo mount -o remount,size=SIZE /run/shm
If you want to create a new partition you must first create the folder where the partition will be mounted (if not already exist)
mkdir -p /tmp/memory
then mount a file system there with the command
mount -t tmpfs -o size=512M,mode=0744 tmpfs /tmp/memory
With this command you specify the size and with mode the standard permissions.
If you want this partition automatically mounted at boot, add everything in fstab
tmpfs /tmp/memory tmpfs size=512M,mode=0744 0 0
And verify in the usual way that the change has been properly applied to the system (with remount and df, as above). I want to remember, however, that when this partition is removed (and this applies also to shutdown/restart of your pc), all the data that it contains will be systematically lost, because in fact it does not create any files on the hard drive, but it only works on RAM . So beware!
Tux + Firefox
Using the shared memory with Firefox
Once you have a ramdisk, make a note of the path (you can also use /run/shm to do a test) and type in the address bar of firefox about:config , and press Enter to confirm.
You'll receive a friendly message "This might void your warranty", press the button "I'll be careful, I promise." You will see a grid that contains all the modifiable parameters of Firefox (many of these are modified by the menu Edit/Preferences, others are modified according to the operating system and in bold there are those who, in the Status column have a Custom value).
In the search bar, type browser.cache.disk and press enter: you should see all the entries starting with this string.
So you should see also an entry for browser.cache.disk.parent_directory , please right click on New/String and in the window that appears just write browser.cache.disk.parent_directory, and press OK and in the next window write the path to your RamDisk (I've put to /run/shm), press OK again to confirm.
If the window is not updated, try to start again from browser.cache.disk.
Now we have to change other values, such as browser.cache.disk.capacity that is used to set the maximum amount of cache firefox uses. This value should be adjusted according to the size of your RamDisk: if it is 512MB and you want to use it all for the browser, then you must multiply this value by 1024 (to get the KyloByte).
Now do a right click on browser.cache.disk.capacity and change the current value with what you have just calculated (advise you to never use all the ramdisk, but put a little less, in this case instead of 512 is better to use 500).
Make sure browser.cache.disk.enable is set to true, if it is not just do a right click and choose Setup, or double-click the entry to change the value from false to true and vice versa.
Once this is done, restart your browser and navigate a bit (even just open google). Now open a terminal, go into your RamDisk directory and verify that there is a folder called Cache and that there are present in it other folders and files.
You should now see some improvement, but beware! When you restart the computer, all the navigation data will be lost! And this is great for privacy.
This Hub was last updated on December 12, 2012
You can help the HubPages community highlight top quality content by ranking this article up or down.