Tuesday, February 09, 2010

In memory FileSystem

Linux(tmpfs and ramfs)
In Linux, you can convert part of your physical memory to be used as a disk partition. In this partition you can read/write files just like any other file on disk, but as the read/writes are done in memory they are really fast.

This partition is useful for applications which at run-time create lots of temporary files & have high IO on these files. For example, installers, extractor tools.

In Linux, we have tmpfs and ramfs mounts that provide the power to create in-memory file system for fast reading and writing files from and to the primary memory.

How to create and mount tmpfs

# mkdir -p /mnt/tmpfs


# mount -t tmpfs -o size=200m tmpfs /mnt/tmpfs
This will create a tmpfs partition of 200MB.

You can view the partition with
# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 125641908 99794556 19515336 84% /
tmpfs 1557024 40000 1517024 3% /lib/modules/2.6.24-27-generic/volatile
tmpfs 204800 0 204800 0% /tmp/tmpfs


How to create and mount ramfs

# mkdir -p /mnt/ramfs

# mount -t ramfs -o size=200m ramfs /mnt/ramfs

You can view the partition with
# mount

/dev/sda3 on / type ext3 (rw,relatime,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)

/sys on /sys type sysfs (rw,noexec,nosuid,nodev)

varrun on /var/run type tmpfs (rw,noexec,nosuid,nodev,mode=0755)

varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)

udev on /dev type tmpfs (rw,mode=0755)

devshm on /dev/shm type tmpfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

lrm on /lib/modules/2.6.24-23-generic/volatile type tmpfs (rw)

securityfs on /sys/kernel/security type securityfs (rw)

none on /proc/fs/vmblock/mountPoint type vmblock (rw)

tmpfs on /lib/modules/2.6.24-27-generic/volatile type tmpfs (rw,mode=0755)

tmpfs on /tmp/tmpfs type tmpfs (rw,size=200m)

ramfs on /mnt/ramfs type ramfs (rw,size=200m)



ramfs vs tmpfs
Basically the same with minor differences:
* ramfs can grow dynamically, tmpfs can not.
This means that in ramfs you can keep on writing beyond the maximum size of your partition, the system will not stop you. So you need your application to keep track of the size.
Where as, tmpfs will not grow dynamically. It may give errors similar to “No space left on device”, when you hit the maximum size of partition.

* tmpfs uses swap, ramfs doesn't.

* Both are volatile file system. So on system shutdown/crash you lose the data.

Microsoft Windows
Windows systems have a rough analog to tmpfs in the form of "temporary files". Files created with both FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE are held in memory and only written to disk if the system experiences low memory pressure. In this way they behave like tmpfs, except the files are written to the specified path during low memory situations rather than swap space.

No comments: