[How To] Create a RAM disk in Linux
There are many reasons for creating a memory based file system in Linux, not least of which is to provide a near zero latency and extremely fast area to story files. A prime use of a RAM disk is for application caching directories or work areas.
There are two main types of RAM disk which can be used in Linux and each have their own benefits and weaknesses:
See my other post for the differences between ramfs and tmpfs.
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.
The free command shows the amount of RAM availale on your system in addition to the amount of memory used, free and used for caching. SWAP space is also displayed and shows if your system is writing memory to disk.
Create a folder to use as a mount point for your RAM disk.
Then use the mount command to create a RAM disk.
mount -t [TYPE] -o size=[SIZE] [FSTYPE] [MOUNTPOINT]
Substitute the following attirbutes for your own values:
- [TYPE] is the type of RAM disk to use; either tmpfs or ramfs.
- [SIZE] is the size to use for the file system. Remember that ramfs does not have a physical limit and is specified as a starting size.
- [FSTYPE] is the type of RAM disk to use; either tmpfs, ramfs, ext4, etc.
You can add the mount entry into /etc/fstab to make the RAM disk persist over reboots. Remember however, that the data will disappear each time the machine is restarted.