How to create a RAM disk

Jan 03 2016

The Problem

Sometimes data has to be written and read ultimately fast. The random access memory is one of the fastest memories within a computer - it is not big, but fast. A cheap and handy solution is to create a tmpfs filesystem with a command like #mount -t tmpfs -o size=20% none /tmp/ramdisk - but this has a major drawback: As soon as the RAM get full data is beeing swapped out to the disk - which may be counterproductive.

The Solution

So if you would like to read and store data data with the speed of the RAM then you can mount a part of your RAM as disk where you can place files on. The creation is really simple: First you have to create a mountmoint and afterwards you can mount your RAM disk with the ramfs there. The ram file syste is present within the linux sources since Linux 2.3.99pre4.

$ mkdir ramdisk
$ sudo mount -t ramfs -o size=8G,mode=1777 ramfs ./ramdisk/

The RAM disk in the upper example has a size of max. 8GB and can be read and written from everybody on the system.So lets have a look at the speed of this disk:

$ sync ; time sh -c "dd if=/dev/zero of=./ramdisk/testfile bs=100k count=1k  && sync" ; rm ./ramdisk/testfile
1024+0 records in
1024+0 records out
104857600 bytes (105 MB) copied, 0,0247143 s, 4,2 GB/s

real 0m0.040s
user 0m0.000s
sys 0m0.028s

I used the dd utility to create 1000 blocks of 100k size (which totals to 105 MB). The sync commands are forcing the kernel to write the data to the disk (so we can be sure that nothing is in some caches anymore). As we can see the write speed is about 4.2 GB/s -> Yes that is a whole DVD within one second !

After you have finished using the RAM disk you have to unmount it again. this can be done via the umount command. The following snipplet does exactly this, and it also shows that a RAM disk only consumes that much RAM as the content within the ramdisk needs.

$ free -h
             total       used       free     shared    buffers     cached
Mem:           11G       5,6G       5,8G       425M        88M       3,3G
-/+ buffers/cache:       2,2G       9,2G
Swap:          11G       108M        11G

$ sudo umount ./ramdisk
$ free -h
             total       used       free     shared    buffers     cached
Mem:           11G       5,5G       5,9G       426M        88M       3,2G
-/+ buffers/cache:       2,2G       9,2G
Swap:          11G       108M        11G

One can see that the free memory before the unmount was 5.8GB and after the unmount it was 5.9GB. The difference is exactly the 105 MB chunck that was written via the dd command to test the performance.

If you are not happy with a fixed disksize then you can tell the mount command to use 50% of your RAM.

$ sudo mount -t ramfs -o size=50%,mode=1777 ramfs ./ramdisk/

This would create (on my machine) a 6.5GB RAM disk, It should be mentioned that content of a RAM disk is never swapped out to the swap disk (if available) - so whatever is stored on the RAM disk is deleted after a reboot.

Get Social

(c) 2017, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen