The Difference Between a tmpfs and ramfs RAM Disk
Category : Knowledge
There are two file system types built into most modern Linux distributions which allow you to create a RAM based storage area which can be mounted and used link a normal folder.
Before using this type of file system you must understand the benefits and problems of memory file system in general, as well as the two different types. The two types of RAM disk file systems are tmpfs and ramfs and each type has it’s own strengths and weaknesses.
See my other post for details on how to create a RAM disk in Linux.
What is a memory based file system (RAM disk)?
A memory based file system is something which creates a storage area directly in a computers RAM as if it were a partition on a disk drive. As RAM is a volatile type of memory which means when the system is restarted or crashes the file system is lost along with all it’s data.
The major benefit to memory based file systems is that they are very fast – 10s of times faster than modern SSDs. Read and write performance is massively increased for all workload types. These types of fast storage areas are ideally suited for applications which need repetitively small data areas for caching or using as temporary space. As the data is lost when the machine reboots the data must not be precious as even scheduling backups cannot guarantee that all the data will be replicated in the even of a system crash.
tmpfs vs. ramfs
The two main RAM based file system types in Linux are tmpfs and ramfs. ramfs is the older file system type and is largely replaced in most scenarios by tmpfs.
ramfs
ramfs creates an in memory file system which uses the same mechanism and storage space as Linux file system cache. Running the command free in Linux will show you the amount of RAM you have on your system, including the amount of file system cache in use. The below is an example of a 31GB of ram in a production server.
free -g total used free shared buffers cached Mem: 31 29 2 0 0 8 -/+ buffers/cache: 20 11 Swap: 13 6 7
Currently 8GB of file system cache is in use on the system. This memory is generally used by Linux to cache recently accessed files so that the next time they are requested then can be fetched from RAM very quickly. ramfs uses this same memory and exactly the same mechanism which causes Linux to cache files with the exception that it is not removed when the memory used exceeds threshold set by the system.
ramfs file systems cannot be limited in size like a disk base file system which is limited by it’s capacity. ramfs will continue using memory storage until the system runs out of RAM and likely crashes or becomes unresponsive. This is a problem if the application writing to the file system cannot be limited in total size. Another issue is you cannot see the size of the file system in df and it can only be estimated by looking at the cached entry in free.
tmpfs
tmpfs is a more recent RAM file system which overcomes many of the drawbacks with ramfs. You can specify a size limit in tmpfs which will give a ‘disk full’ error when the limit is reached. This behaviour is exactly the same as a partition of a physical disk.
The size and used amount of space on a tmpfs partition is also displayed in df. The below example shows an empty 512MB RAM disk.
df -h /mnt/ramdisk Filesystem Size Used Avail Use% Mounted on tmpfs 512M 0 512M 0% /mnt/ramdisk
These two differences between ramfs and tmpfs make tmpfs much more manageable however this is one major drawback; tmpfs may use SWAP space. If your system runs out of physical RAM, files in your tmpfs partitions may be written to disk based SWAP partitions and will have to be read from disk when the file is next accessed. In some environments this can be seen as a benefit as you are less likely to get out of memory exceptions as you could with ramfs because more ‘memory’ is available to use.
See my other post for details on how to create a RAM disk in Linux.
4 Comments
minal
18-Feb-2015 at 12:40 pmThanks for the shared information!
But, I want to ask few other things regarding the tmpfs implementation for my raspberry pi device.
I currently have my /etc/fstab content as follows :
tmpfs /tmp tmpfs nodev,nosuid,noexec,nodiratime,size=10M 0 0
tmpfs /var/lib tmpfs nodev,nosuid,noexec,nodiratime,size=10M 0 0
tmpfs /var/log tmpfs nodev,nosuid,noexec,nodiratime,size=10M 0 0
tmpfs /var/run tmpfs nodev,nosuid,noexec,nodiratime,size=10M 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 ro 0 1
I want to create Read only FS for my device.
I want to ask, whether the implemented thing is correct or not?
I am facing some errors at boot time, when my device is booting first time, with having it’s Readonly FS changes in it, device is not enabling with dhcp, I mean, /etc accessing these files for RW purpose and because of making complete / partition as “ro” I am facng these issues,
So, can you please help me in this if you have any idea?
Thanks in advance!
Ram P
12-Jul-2016 at 8:05 amGood article, it saved my time :)
Servesha Dudhgaonkar
17-Jul-2018 at 5:48 amGood information that I came across. In rhel7 we happened to see the tmpfs path as /dev/shm
[root@desktop6 ~]# df -h /dev/shm/
Filesystem Size Used Avail Use% Mounted on
tmpfs 921M 80K 921M 1% /dev/shm
JesusDF
17-Jul-2019 at 9:40 pmIt is possible to set a fixed size for ramfs with a kernel parameter: ramdisk_size=512M
Also the main difference is that tmpfs allocates memory on the fly, so it tends to fragment the ram a lot, while ramfs allocates all the memory in a single block.