Creating and Utilizing a RAM Disk

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
348
Reaction score
390
Credits
12,246
With today's hardware speeds, there may not be a need for a RAM Disk. If anyone is still using slow hard disks instead of SSD or NVMe drives, then this may be handy.

Also, on a database system, you can use this to store the database in RAM for speed. Of course, this is not recommended, but it can work.

Background

Even if you look back at the days of the Disk Operating System (DOS), there was the use of RAM disks.

Some people may be asking, "What is a RAM Disk?". Well, let's answer that first.

A RAM disk is using a portion of your RAM to imitate a disk drive. Since RAM is faster than a hard disk drive (HDD), you can expect a better response.

I recall a technician compressing all of his needed files into a compressed Zip file. He placed the Zip file and needed boot files onto a floppy disk (yes, I said floppy disk - this has been a while). After booting from the diskette, the system would create a RAM disk and uncompress the Zip file to the RAM disk. Once this was completed, he had all his tools in RAM and could use any of them without copying them to the PC. His main reason was not the speed, but the fact that all his tools were larger than the diskette. When compressing the files, he used the highest compression rate. Compressing the files took some time to do, but he got the file size down to a suitable size. After the file was compressed, it didn't take long at all to uncompress it.

Installation

For the RAM disk I am using, you need to compile it from the source. To start, you need to install a few necessary files:

sudo apt install libjansson-dev libpcre3-dev libdevmapper-dev libmicrohttpd-dev

And, if you don't have them already:

sudo apt install make gcc

Go to the site 'https://github.com/pkoutoupis/rapiddisk/' and download the code in ZIP format. Extract the files when it is downloaded and place them somewhere accessible, such as in your Home folder.

Open a terminal in the new folder where the files are extracted. Run the following commands:

make
sudo make install
sudo make tools-install


After it compiles and installs, we need to install the kernel modules so the daemon can be executed. Perform the commands:

sudo modprobe rapiddisk
sudo modprobe rapiddisk-cache


Now, we need to start the daemon and enable it to start when the system boots:

sudo systemctl start rappiddiskd
sudo systemctl enable rapiddiskd


You can reboot at this time to make sure everything is loaded and running.

Create the RAM Disk

Everything should be loaded and running, so now it is time to create the RAM disk.

To see what memory is available, use the 'free' command in a terminal. Do not use all of the remaining memory when creating a RAM disk. RAM will need to be left available to allow for the OS to run and if new services or programs are started.

In my case, I have 16 GB available, so I'll make a 4 BG RAM disk with the command:

sudo rapiddisk -a 4096

The block device created will be named 'rd0' since it is the first RAM disk. If others are created, they will be numbered consecutively. A message should be returned that the disk has been created, as follows:

rapiddisk 9.1.0
Copyright 2011 - 2023 Petros Koutoupis

Attached device rd0 of size 4096 Mbytes.


If you perform the command 'ls /dev/rd*', you should see the list of all RAM disks. Another command is 'sudo rapiddisk -l'.

Since the device is created, we need to format it.

You can use GParted, or do it from a command-line. An example to format the RAM disk as 'ext3' would be:

sudo mkfs.ext3 /dev/rd0

You can use any supported file format on your system, to see those supported, use the command:

cat /proc/filesystems | grep -v nodev

Use any supported disk format listed instead of 'ext3' as shown in the example.
We need to mount the disk to a local folder to make it a little more usable. I created a folder called '/media/jarret/rdisk'. I then mapped 'rd0' to it with the command 'mount /dev/rd0 /media/jarret/rdisk'.
The disk is now ready to use.

Testing the RAM Disk

You can open 'Disks', or from a terminal start 'gnome-disks'.
In the left pane, select your RAM Disk. For example, it says:

4.3 GB Block Device
/dev/rd0


On the right side of the window at the top, is an icon with three vertical dots. Click this, then select 'Benchmark Disk…' from the drop-down list.
A new window pops up and you can select 'Start Benchmark...'. On the new window, you can make changes as you need, but do not check the option for a 'Perform write-benchmark'. If you check it, the program will try to unmount the drive, which the 'rapiddisk' daemon will not allow and fail.
Once you have made any changes, or accept the defaults, select 'Start Benchmarking…". You should be prompted for your password to use elevated privileges. The benchmark will be performed, as the results are shown in Figure 1.

Figure 1.JPG

FIGURE 1

The graph shows my average read rate is 12.2 GB/s. As a comparison, my hard drive averages 518.5 MB/s. This shows that my RAM is more than twice as fast as my hard disk. The average access time on the hard disk was 0.55 ms, where RAM was 0.00 ms.
To remove the RAM disk, you use the command 'rapiddisk -d <RAMdisk>'. In the example above, I would use 'rapiddisk -d rd0'.
Now we know how to make a RAM disk and use it as a drive or mapped folder, but what if we want the RAM to cache the hard disk?

Caching a Hard Disk with RAM

We can use RAM to cache data from the hard disk, SSD, or other. Doing this can enhance performance for disk reads and writes.
To accomplish this, we need to create the RAM Disk, similar to the above. The command 'sudo rapiddisk -a 4096'. The RAM disk is created like before, but now we need to associate it with a drive. You cannot use your drive that has the Operating System (OS) on it since the drive needs to be unmounted. You can see in Figure 2 that I tried to connect the 'rd0' drive to 'sdb', but 'sdb' was mounted. I unmounted the 'sdb' drive and then cached the drive with our 'rd0', which mounted the two drives together. To cache a drive, the command is:

sudo rapiddisk -m <RAMdisk> -b <unounted-drive-to-cache>

Figure 2.JPG

FIGURE 2

So, in my example, the command is:

sudo rapiddisk -m rd0 -b /dev/sdc

NOTE: I changed to a different system and the USB changed to '/dev/sdc/. That's why there's a difference in the image in Figure 2 and later images.

When you make the cache drive, a name is given in the output, but you can also see it in Figure 3. This is the cached drive, 'rc-ws_sdc'.

Figure 3.jpg

Figure 3

In my case, '/dev/sdc', is a USB drive. Let me run benchmarks on it. With the cache enabled, I get an average read rate of 377.7 MB/s and an access time of 0.37 ms. This is shown in Figure 4.

Figure 4.jpg

FIGURE 4

Let me disable the cache with the command 'sudo rapiddisk -u rc-wt_sdc'. The RAM disk is unmounted from being a cache drive.
The specs for the uncached drive are shown in Figure 5. They are an average read rate of 312.4 MB/s and an average access time of .36 ms. The average write rate did increase without cache, but the read time was improved.

Figure 5.jpg

FIGURE 5

The reason that the write speed is slower with cache is that writing data always takes longer and you are writing it to cache and then also writing it to the disk.
I should mention that the drive I was caching was an external SSD.

Conclusion

If you have a slow drive and quite a bit of RAM, give Rapid Disk a try. You may be surprised at the improvement, depending on the drive and RAM speed.
Even if you have a file you need to access at better speeds, you can create a RAM disk and map it to a folder. Then you copy the file onto the RAM disk and access it there. If you make changes to the file, be sure to copy the file back to your drive because the RAM disk is volatile memory. Meaning, anything on it is lost when the system is turned off.
 


Boy, that brings back some memories! The last time I used a ram disk was using the extra 168k of ram on my old Tandy 1400LT. It made programs FLY!
 


Top