tmpfs与ramfs

 

URL:http://www.eit.name/blog/read.php?426

Overview:

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 andramfs 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:
# cp 20mb.tgz /mnt/tmp
cp: writing `/mnt/tmp/20mb.tgz’: No space left on device.

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. 

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 better, frankly it’s mostly used to store php sessions.

你可能感兴趣的:(File,command,System,processing,disk,Intervals)