Overview of RAMFS and TMPFS on Linux

by Ramesh Natarajan on November 6, 2008

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.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 17 comments… read them below or add one }

1 aa November 6, 2008 at 9:33 am

Ramfs uses swap. ?

2 WB November 7, 2008 at 7:05 am

Ramfs uses swap.??

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

3 Prune November 26, 2008 at 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…

4 Ramesh November 26, 2008 at 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.

5 k3ninho December 10, 2008 at 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.

6 Frank March 10, 2009 at 12:29 pm

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

F

7 Ramesh April 1, 2009 at 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

8 johnsfine April 2, 2009 at 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.

9 Leslie Satenstein June 28, 2009 at 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?

10 tom3k August 26, 2009 at 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…

11 vennila October 27, 2009 at 6:51 am

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

12 Anonymous October 30, 2009 at 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?

13 performance July 4, 2011 at 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?

14 trex February 20, 2012 at 3:38 am

Thanks!

15 kanth August 8, 2012 at 1:11 am

great article! very useful! thanks a lot.

16 Niyiru May 2, 2013 at 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!!!

17 CBee June 6, 2013 at 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.

Leave a Comment

Previous post:

Next post: