Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Keep /tmp in RAM

This tutorial will show you how to turn your /tmp directory into a "RAM disk". Essentially this means that any file stored in /tmp is actually being stored in memory rather than on your persistent storage devices.

Before starting, it is worth explaining that:

  • Accessing content from RAM is much faster than the fastest SSDs in terms of both latency and throughput/bandwidth.
  • Data held in memory is lost whenever it loses power. This is why it is often referred to as volatile storage.
  • The /tmp is a temporary directory for content that will be removed at the next reboot. Hence it doesn't matter if it is suddenly lost due to a sudden unexpected loss of power as would happen with anything stored in RAM.


Decide How Much Ram To Use

Start a typical "heavy" workload. E.g. open 100 chrome tabs, play some music in the background, and open up a game. Now find out how much memory is free by using the command free -m. My output is as shows:

             total       used       free     shared    buffers     cached
Mem:         15707      13458       2249       4833        701       8453
-/+ buffers/cache:       4303      11403
Swap:         3905          0       3905

The figures are listed in MB. In this case we have 11,403 MB of RAM that is available to play with which is around 10.5 GiB. This looks a lot larger than the figure of 2249 but that is because my machine is currently using a lot of the "spare" ram for buffers/caching and not for processes.

We now know how much RAM we typically have lying around being "unused". You can decide based on this number what the cap should be on your RAM disk. You may want to use a figure between 75% and 100% of this figure. Obviously the more RAM you have the less likely you are to ever hit this cap. Running out of RAM will cause your computer to use swap which will seriously degrade performance so it is important to keep enough space spare for your processes, and also to not fill your /tmp area. Only you can find the sweet-spot, which is made less difficult by having lots of RAM. 4 X 8GB will only set you back £130.

I have decided to set the cap at 10240M (10GB). This is because I feel that I am very unlikely to hit this cap and this doesn't result 10GB of RAM being used unless I fill /tmp (not pre-allocated).

Update Fstab

Simply add the following line to /etc/fstab and substitute [CAP] with how many MB you wish to set your memory limit at. You may want to add a comment above to explain what it's there for.

tmpfs /tmp tmpfs defaults,noatime,nosuid,nodev,mode=1777,size=[CAP]M 0 0

Now reboot. You could run sudo mount -a for the changes to take immediate effect, but this could cause issues as items currently being used in /tmp would "disappear".


If you want to see how much difference this will make, run the following script from inside /tmp and also from inside a normal folder.

echo "writing 1GB sequentially"
dd if=/dev/zero of=1GB.img bs=1M count=1000 oflag=dsync

You should see a massive difference in performance, even if you use an SSD. This is because the script will write 1GB using 1 MB chunks whereby each chunk can only be written once the previous one has been successfully written (no buffers), rather than trying to just write 1GB of data all at once.


Now anything you copy into /tmp can be accessed instantly. This could be great for video editing, custom-built applications that would benefit from fast I/O, or copying game files to for instant loading. Just remember that all this data will be lost if there is a power-cut or you reboot so it's better to copy files rather than move them.


Last updated: 11th August 2022
First published: 16th August 2018