How to Create a RAM Disk

In some cases, setting up a memory-based file system may be preferable, as it allows memory on a server to be used like a disk partition. This is an excellent option for caching files and other temporary data, since RAM is faster than using physical disk space. There are two types of RAM disks that can be used:

  • ramfs
  • tmpfs

Ramfs creates uses the same method of storing files as the Linux file system cache, with the downfall that it will continue using memory storage until the system runs out of RAM, which can cause the system to crash or become unresponsive. Tmpfs, however, behaves like a physical partition on the disk and is capable of running out of space. With that said, it’s also possible for it to occupy swap space if the server runs out of RAM, possibly defeating the purpose of using a ramdisk altogether.

Before creating a RAM disk, check how much RAM you generally have available on your machine. You can use ‘free’ to see the current usage, or ‘sar -r’ to see historical usage. You’ll want to make sure you have enough free RAM on your system to accommodate a RAM disk.

Check the amount of free RAM you have left on your machine before creating a RAM disk. Use the Linux command free to see the unused RAM. The below is an example of a 31GB of ram in a production server.

Next, create a folder to use a a mount point, for example:

mkdir /mnt/ramdisk

Then mount it:

mount -t [tmpfs|ramfs] -o size=1024m [tmpfs|ramfs] /mnt/ramdisk

Using ‘ramfs’ as a type will denote ‘size’ as a starting size only, since it does not have a physical limit. The above example creates a 1GB ramdisk.
To retain the mountpoint over reboots, you should add this to /etc/fstab as well. Keep in mind that since this is data stored in RAM though, anything stored here will be erased every time the server is rebooted.

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