≡ Menu

Overview of RAMFS and TMPFS on Linux

This is a guest post written by SathiyaMoorthy.

[Linux Ramfs and Tmpfs]Using ramfs or tmpfs you can allocate part of the physical memory to be used as a partition. You can mount this partition and start writing and reading files like a hard disk partition. Since you’ll be reading and writing to the RAM, it will be faster.

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.

1. How to mount Tmpfs

# mkdir -p /mnt/tmp

# mount -t tmpfs -o size=20m tmpfs /mnt/tmp

The last line in the following df -k shows the above mounted /mnt/tmp tmpfs file system.

# df -k
Filesystem      1K-blocks  Used     Available Use%  Mounted on
/dev/sda2       32705400   5002488  26041576  17%   /
/dev/sda1       194442     18567    165836    11%   /boot
tmpfs           517320     0        517320    0%    /dev/shm
tmpfs           20480      0        20480     0%    /mnt/tmp

2. How to mount Ramfs

# mkdir -p /mnt/ram

# mount -t ramfs -o size=20m ramfs /mnt/ram

The last line in the following mount command shows the above mounted /mnt/ram ramfs file system.

# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
tmpfs on /mnt/tmp type tmpfs (rw,size=20m)
ramfs on /mnt/ram type ramfs (rw,size=20m)

You can mount ramfs and tmpfs during boot time by adding an entry to the /etc/fstab.

3. Ramfs vs Tmpfs

Primarily both ramfs and tmpfs does the same thing with few minor differences.

  • Ramfs will grow dynamically.  So, you need control the process that writes the data to make sure ramfs doesn’t go above the available RAM size in the system. Let us say you have 2GB of RAM on your system and created a 1 GB ramfs and mounted as /tmp/ram. When the total size of the /tmp/ram crosses 1GB, you can still write data to it.  System will not stop you from writing data more than 1GB. However, when it goes above total RAM size of 2GB, the system may hang, as there is no place in the RAM to keep the data.
  • Tmpfs will not grow dynamically. It would not allow you to write more than the size you’ve specified while mounting the tmpfs. So, you don’t need to worry about controlling the process that writes the data to make sure tmpfs doesn’t go above the specified limit. It may give errors similar to “No space left on device”.
  • Tmpfs uses swap.
  • Ramfs does not use swap.

4. Disadvantages of Ramfs and Tmpfs

Since both ramfs and tmpfs is writing to the system RAM, it would get deleted once the system gets rebooted, or crashed. So, you should write a process to pick up the data from ramfs/tmpfs to disk in periodic intervals. You can also write a process to write down the data from ramfs/tmpfs to disk while the system is shutting down. But, this will not help you in the time of system crash.

Table: Comparison of ramfs and tmpfs
Experimentation Tmpfs Ramfs
Fill maximum space and continue writing Will display error Will continue writing
Fixed Size Yes No
Uses Swap Yes No
Volatile Storage Yes Yes


If you want your process to write faster, opting for tmpfs is a better choice with precautions about the system crash.

This article was written by SathiyaMoorthy. He is working at bksystems, interested in writing articles and  contribute to open source in his leisure time. The Geek Stuff welcomes your tips and guest articles.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • aa November 6, 2008, 9:33 am

    Ramfs uses swap. ?

  • WB November 7, 2008, 7:05 am

    Ramfs uses swap.??

    what is the advantage from ramfs? is it older than tmpfs?

  • Prune November 26, 2008, 4:42 am

    Hi,

    It’s cool to use articles from other blogs, and give link.
    What is even cooler is to CORRECT IT (or maybe you add the error yourself copying ? :)).

    You first say RamFS use SWAP and not TmpFS.
    Then, in the summary, you say the contrary…

    Which one is true ?
    I will let you figure that out…

  • Ramesh November 26, 2008, 1:56 pm

    @aa, @WB, @Prune,

    Ramfs does not use swap. I’ve corrected it in the article. Like Prune mentioned, while copying the guest article and formatting it, I made a mistake. I apologize for the mistake and sincerely appreciate your help in pointing out the issue.

  • k3ninho December 10, 2008, 9:13 am

    Ramesh,

    Can you also correct the table (Comparison of ramfs and tmpfs)? The reference to Non-Volatile Storage doesn’t make sense. In the standard sense of volatile or non-volatile storage, RAM used in both RAMFS and TMPFS is volatile because when it loses power, it also loses data.

    K3n.

  • Frank March 10, 2009, 12:29 pm

    How do these options differ from simply using /dev/shm?

    F

  • Ramesh April 1, 2009, 3:10 pm

    @k3ninho,

    Thanks for pointing it out. I’ve corrected the comparison table.

    @Frank

    dev/shm is the typical shared memory. You can use it to pass application data between various process. Following link has an interesting discussion thread on this topic:

    http://www.mail-archive.com/lfs-chat@linuxfromscratch.org/msg01251.html

  • johnsfine April 2, 2009, 8:34 am

    I think you had too much discussion of copying data to disk periodically or on shutdown. Tmpfs or ramfs are mostly used for data that you would never want after the next reboot. If it is worth the time/trouble of backing up to disk, it is worth the time to create it on disk in the first place. Trust the system level caching to make better decisions than you can make manually about buffering writes in ram for speed with delayed writes to disk soon enough for safety.

  • Leslie Satenstein June 28, 2009, 11:49 am

    Currently without a ramdisk, my /tmp is getting filled with deletable files (delete on next boot).

    By creating the tmpfs as above, what will direct applications to it?

    Otherwise, if it is automatic, how can I benefit from it in a system startup situation?

  • tom3k August 26, 2009, 1:46 pm

    iv been seeing alot about a mount paramater “maxsize” for ramfs…

    does anyone know if this paramater exists?

    i would like basically the features of ramfs (non swap, growing…) but without the hazard of having it fill my ram to 100%.

    im using it for my mysql temp folder…

  • vennila October 27, 2009, 6:51 am

    I got good information about tmpfs and ramfs.
    Thank you!

  • Anonymous October 30, 2009, 1:01 pm

    Deleting files on tmpfs does not seem to free the space instantly. It takes about 15 minutes for the freed space to appear on “df”. Anything we could do to free the space instantly?

  • performance July 4, 2011, 1:58 pm

    What about performance comparison?
    Which one uses smaller CPU?
    As far as I know ramfs uses much simple kernel code – which theoretically makes it faster?

  • trex February 20, 2012, 3:38 am

    Thanks!

  • kanth August 8, 2012, 1:11 am

    great article! very useful! thanks a lot.

  • Niyiru May 2, 2013, 9:06 pm

    How tmpfs uses swap??, it flushes to it from time to time??, can you help me to understand?.

    Thanks for this excellent article!!!

  • CBee June 6, 2013, 8:58 am

    @Niyiru: tmpfs uses swap just like roughly every other application: once the memory runs below a level and there is swap-space, the less-used blocks can/will be moved out.

    @anonymous: For most filesystems: once you delete files, their entries in the file-name-table is instantly removed. The inode is only removed once the last link to it is removed (including filepointers from processes). This can be instant or on a ‘garbadge collecting’ moment. Only after that, the data space is given to the free-blocks list. Which in turn waits for its moment to be free-ed or re-used.

  • minal February 18, 2015, 6:37 am

    Thanks 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!

  • andrew February 27, 2015, 6:26 am

    Johnshine,prtivacy FIRST! I don’t want any data to be recovered.what it type,what and when I save,when i did and how…etc. So you have to try and think a little out of the box before you post your ignorance…

  • Arief May 16, 2017, 6:24 pm

    How about if I use both ramfs and tmpfs in the same time.
    Is it problem ?

  • Leslie Satenstein May 19, 2017, 8:07 am

    No example was given for having tmpfs within fstab.

    Here is an example for you to use tmpfs with fstab
    tmpfs /tmp tmpfs nodev,nosuid,noatime,size=1G 0 0