Tag Archives: memfs v/s tmpfs

How to create RAM disk in Linux

Short tutorial explaining what is RAM disk and how to create a RAM disk in Linux. It also includes differences between ramfs and tmpfs.

RAM disk in Linux

Recently one of our readers asked “how to create a RAM disk in Linux?”. So I thought of writing this small tutorial which will help you to understand what is a RAM disk, what is the use of it and how to create a RAM disk in Linux.

What is RAM disk?

Roughly RAM disk can be termed as a portion of your RAM mounted as a directory. It uses tmpfs or ramfs. Refer below table for the difference between ramfs and tmpfs.

ramfs
tmpfs
Old type New type and replacing ramfs now
Can not be limited in size Size limit can be defined
Since can not be limited may lead to system crash Once limit reached, disk full error written. No system crash issue.
Entry is not visible in ‘df’ output. Need to calculate by using ‘Cached’ number in ‘free’ output. Can be seen in ‘df’ command output
Work mechanism as file system cache Work mechanism is as partition of physical disk

RAM disk is a very high speed, high performance and almost zero latency area to store application files. Due to its performance-oriented nature, its mostly used for temporary data like caching application files.

How to create RAM disk?

RAM disk can be created in simple two steps. One is to create a directory on which it should be mounted and the second step is to mount it on that directory using specific FS type. Make sure you have enough free RAM on the system so that portion of it can be used in RAM disk. You can check it using free command.

Lets create directory /mnt/ram_disk and mount RAM disk on it.

# mkdir /mnt/ram_disk
# mount -t tmpfs -o size=1024m new_ram_disk /mnt/ram_disk

In above mount command, -t should be followed by tmpfs or ramfs type. For ramfs, size is the starting size of RAM disk since ramfs have limitless size. Size followed by the name of the disk (of your choice ex. new_ram_disk). You can verify if it mounted properly using df command.

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      5.8G  2.9G  2.7G  53% /
tmpfs           498M     0  498M   0% /dev/shm
new_ram_disk          1.0G     0  1.0G   0% /mnt/ram_disk

You can see newly created tmpfs of 1GB size is mounted on /mnt/ram_disk (highlighted above).

You can add below entry in /etc/fstab as well to persist it over reboots as well. But keep in mind that data within RAM disk flushes for each reboot since its backed memory is volatile.

new_ram_disk    /mnt/ram_disk   tmpfs    nodev,nosuid,noexec,nodiratime,size=1024M   0