RAMFS SYSFS and TMPFS on Linux

These three are all in-memory file-system. Sysfs was originally based on ramfs. ramfs was written around the  Linux 2.4 time. It’s an exercise in elegance, as it showed just how easy it was to write simple filesystem using the VFS layer. Because of its simplicity and use of the VFS, it provided a good base from which to derive other in-memory based filesystems.

Using in-memory fs you can allocate part of physical memory to be used as a harddisk partition. You can mount this partition and start writing and reading files like a harddisk partition, it will be much faster for sure. When a vital process becomes drastically slow because of disk writes, you can choose either ramfs or tmpfs file systems for writing files to the RAM.

Both tmpfs and ramfs mount will give you the power of fast reading and writing files from and to the primary memory. When you test this on a small file, you may not see a huge difference. You’ll notice the difference only when you write large amount of data to a file with some other processing overhead such as network.

How to mount Tmpfs and Ramfs

# mkdir -p /mnt/{tmp,ram}
# mount -t tmpfs -o size=50m tmpfs /mnt/tmp
# mount -t ramfs -o size=50m ramfs /mnt/ram

You can mount ramfs and tmpfs during boot time by adding an entry to the /etc/fstab or execute the above mount command when starting your system.

Difference between Ramfs and Tmpfs
Generally ramfs and tmpfs does the same thing with few minor differences.

* Ramfs will grow dynamically. So, you need control the process that writes data to make sure ramfs doesn’t exhaust all avaliable RAM in your system, otherwise an OOM will occure which might cause some potential damage. Assume that you have 2GB of RAM and created a 1 GB ramfs and mounted as /mnt/ram. When the total size of the /mnt/ram crosses 1GB, you can still write data to it. System will not stop you, when it goes above total RAM size of 2GB, the system may hang, as there is no RAM for other operation.

* Tmpfs will not grow automatically. It’s more like a harddisk partition. It does not allow you to write more data to the mounted tmpfs. When you try to write more data the specified limit, It’ll output errors like this:
bash-3.20# cp 20mb.tgz /mnt/tmp
cp: writing `/mnt/tmp/20mb.tgz’: No space left on device

* Ramfs does not use swap while Tmpfs uses.

Ramfs and Tmpfs Disadvantages
Since both ramfs and tmpfs is writing to the system RAM, it would get deleted once the system gets rebooted, or crashed. If the data is critical, you should write a script to pick up the data from ramfs/tmpfs to disk in periodic intervals, and another script to write down the data from ramfs/tmpfs to disk while the system is shutting down. But, this doesnt help you when system crash.

======== Comparison of ramfs and tmpfs =======
Experimentation ——– Tmpfs ———— Ramfs ——–
continue writing —- display error —– continue writing
Fixed Size ————— Yes —————– No ————
Uses Swap ————- Yes —————— No ———–
Non-volatile ———— No —————— No ———–

If you want your process to write faster, opting for tmpfs is better, frankly it’s mostly used to store php sessions.

Share Button

One thought on “RAMFS SYSFS and TMPFS on Linux

Leave a comment

Your email address will not be published. Required fields are marked *