This is a guest post written by SathiyaMoorthy.
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.
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.
Comments on this entry are closed.
Ramfs uses swap. ?
Ramfs uses swap.??
what is the advantage from ramfs? is it older than tmpfs?
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…
@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.
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.
How do these options differ from simply using /dev/shm?
F
@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
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.
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?
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…
I got good information about tmpfs and ramfs.
Thank you!
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?
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?
Thanks!
great article! very useful! thanks a lot.
How tmpfs uses swap??, it flushes to it from time to time??, can you help me to understand?.
Thanks for this excellent article!!!
@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.
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!
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…
How about if I use both ramfs and tmpfs in the same time.
Is it problem ?
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