Skip to content

[Linux] Store the data into RAM disk to speed up the program

Last Updated on 2021-07-01 by Clay

Recently, I encountered a bottleneck in the training of deep learning models. The most important aspect is the speed of training.

I searched some solutions, and I found a solution is very interesting: I can place the data into RAM and speed up the loading time and writing time.

The following I show the results of my experiments, I distinguish them into "load" and "write". My experimental environments are RAM, SSD, HDD.


Mount RAM disk

Using "mount" instruction, we can easily build a RAM disk.

mkdir /tmp/memory
mount -t tmpfs -o size=1G tmpfs /tmp/memory/

We mounted 1GB memory into "/tmp/memory". If we put the data under it, the data is stored in memory.

Confirm the mount is successful.

df -h

Output:

Because I have put in the data, the memory folder is not empty.


Comparison of written

Now we can test how much fast if data in the RAM. I have a file named "test.json".

du -m test.json

Output:

I used the command "cp" to move it to RAM, SSD, HDD from another HDD.

The following are their moving time:

RAMSSDHDD
5.308s5.781s7.025s

In fact, RAM seems to be only faster than SSD, only HDD is obviously slower. By the way, in order not to let Cache affect the speed of my writing files, I also specifically used the following instructions to clean up Cache.

sudo -i
sync; echo 3 > /proc/sys/vm/drop_caches

Maybe my experiment method is wrong.


Comparison of load

For reading speed test, I use Python program to read the "test.json" file just now. Similarly, every time I finish reading, I clear the Cache.

RAMSSDHDD
14.876s14.636s14.883s

This result is really terrible … not as easy to use as I originally thought.

I wonder if there is a problem with my method of operation? I think I might have to study for a while.


Unmount RAM disk

In fact, it is very simple to remove it. Remember the path "/ tmp / memory" I just mounted?

sudo umount /tmp/memory

Just remove it like this.

By the way, Page Cache is doing very well in Linux. If there is no special requirement, it seems that it can run very smoothly without specially creating a RAM Disk. The above is a little personal thoughts.


References

Tags:

1 thought on “[Linux] Store the data into RAM disk to speed up the program”

Leave a Reply