Working with RAM Disk Devices in Linux

Linux users and system administrators alike are always trying to squeeze as much performance as possible out of their system. A big bottleneck for system performance frequently comes from file transfer speeds – that is, system read and write times. Although recent disk technology like SSDs (Solid State Drives) and NVMe (Non Volatile Memory Express) have sped up transfer times significantly over traditional hard drive disks, none can measure up to the speedy performance of a RAM disk.

A RAM disk is virtual storage which is situated directly into the RAM (Random Access Memory) of a system. So, if your system has 32 GB of RAM installed, you could, for example, create 5 GB of storage within that RAM, and use it to store files. The result is that your system would be left with 27 GB of memory (32 – 5 = 27) for other tasks, while the 5 GB you allotted is used as a RAM disk for files.

In this tutorial, we will see how to work with RAM disks on a Linux system. This will not only include the creation of RAM disks, but also everything you need to know about the advantages and drawbacks of working with RAM disks in Linux. Let’s get started!

In this tutorial you will learn:

  • What is a RAM disk and how do they work?
  • How to create a RAM disk in Linux step by step
  • Pros and cons of using a RAM disk
  • How to create a RAM disk in Linux
Working with RAM Disk Devices in Linux
Working with RAM Disk Devices in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

What Is a RAM Disk?




RAM disks are virtual spaces that consume a portion of the computer’s installed RAM. This virtual space can then be used for file storage. The advantage of a RAM disk is that it performs much faster than conventional disk storage mediums like solid state drives and hard drives. This means that file read and write times are extremely fast, creating a much more seamless, performant, and enjoyable experience for users that need to access many big files and want to minimize latency as much as possible.

A RAM disk has one glaring drawback, which is that the file storage is not permanent. In other words, RAM is volatile computer memory, and all data is lost whenever the system is powered off. As such, RAM disks are only helpful in environments where we need to store temporary data and need to have extremely fast access to it in the meantime.

Now, in environments with a lot of RAM and where system reboots are extremely infrequent (think corporate server environments), RAM disks are a crucial part of providing a responsive user experience with serving up requested data in the blink of an eye. This power can also be harnessed by ordinary end users, say if you are working on a video project, compiling large amounts of code, or other type of task that requires frequent access to a large amount of data, or just a lot of small files.

WARNING
Keep in mind that a RAM disk is only suitable for temporary storage. Anything stored on the RAM disk will be lost whenever the computer is powered off or rebooted. Make sure you move all important data off the RAM disk as soon as possible, or only keep duplicate copies of files on there.

Other Notable Pros and Cons




As discussed, the advantage of using a RAM disk is speed. That is about it. Its temporary nature can also be seen as an advantage, since it makes it ideal for temporary storage needs – it even clears the data itself when the computer is powered off. But this can be a disadvantage just as much as it advantageous.

The main drawback of using RAM disks is that the storage is temporary. It would be foolish to store anything important on a RAM disk, unless another copy of the files also exists on permanent storage (hard drive, SSD, flash drive, etc).

Another disadvantage is that it is a very expensive storage. Just a few gigabytes of RAM disk can cost us the same as a terabyte sized hard drive. But, for users with some RAM space to spare, there may be no real disadvantage in lending some of the RAM’s breathing room to a RAM disk virtual storage.

The final disadvantage is that you will need to keep a better eye on system resources while working with RAM disks. This is especially true if you plan to allot a good chunk of your RAM to being a virtual space for the RAM disk. If your system begins processing some hefty tasks and dumps a lot of information into RAM, your computer’s memory can run out rather quickly, and the space you allotted for the RAM disk will not be able to be used for additional breathing room.

How to Create a RAM Disk in Linux

tmpfs (temporary file system) is the file system used for RAM disks. We can use the mount command to create a new RAM disk and specify how much installed memory that we want the RAM disk to consume.

  1. To start off, it would be a good idea to check the amount of installed RAM in your computer, and see how much is currently free. For that, we can simply use the free command the and following options:
    $ free -ght
    
    Using the free command on Linux
    Using the free command on Linux

    The output above shows us that we have 6 GB of RAM that is free to work with. A bad idea would be to go ahead and use all of that for our RAM disk. Instead, let’s just use half, so that the system still has some buffer available for other tasks that may come up.

  2. Next, let’s make a directory where we will mount our RAM disk. You can also use an already existing directory if you want to, assuming that it is empty.
    $ sudo mkdir -p /mnt/ramdisk
    
  3. Now we will use the mount command to create a RAM disk and mount it to the directory we just created. We need to use the -t tmpfs option in order to make the RAM disk. The following command creates a RAM disk that is 3 GB in size:
    $ sudo mount -t tmpfs -o size=3g tmpfs /mnt/ramdisk
    
  4. That is all there is to it. If you want to confirm the RAM disk you just created, you can execute the df command:
    $ df -h
    
    We can see the RAM disk of 3 GB that was just created and mounted
    We can see the RAM disk of 3 GB that was just created and mounted




Now you can begin copying files to the RAM disk. If you used the same commands as we did above, then the RAM disk should be accessible at the /mnt/ramdisk directory.

Closing Thoughts

In this tutorial, we saw how to work with RAM disks on a Linux system. This included learning about how RAM disks work, and what kind of advantages they provide, along with drawbacks that should be kept in mind. Lastly, we saw how to create a RAM disk on Linux.

RAM disks are faster than both SSD and NVMe drives, and especially traditional hard drive disks, but are more costly and only offer temporary storage. Still, for certain purposes, they can be extremely beneficial and cut down on a lot of wait time for end users and server environments.



Comments and Discussions
Linux Forum