TMPFS和RAMFS是两种类型的文件系统,可以动态改变大小。非传统的虚拟磁盘,而传统的虚拟磁盘是个块设备。需要mkfs之后才可以使用。
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 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.
How to mount Tmpfs and Ramfs
# mkdir -p /mnt/{tmp,ram}
# mount -t tmpfs -o size=50m tmpfs /mnt/tmp (mount命令的使用mount -t type dev dir)
(然后通过df命令或者mount命令查看是否已经mount上,其它用法
mount -o size=1500M -o nr_inodes=1000000(设innodes) -o remount /dev/shm )
# mount -t ramfs -o size=50m ramfs /mnt/ram
(然后通过mount命令查看是否已经mount上)
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.可以通过在/etc/fstab文件中或者在系统启动时执行上述两条命令。
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.RAMFS类型的文件系统,即使使用的空间超过设置,仍然可以继续使用内存,直到内存耗尽。
* 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.TMPFS类型的文件系统,可以使用的空间受到之前设置的限制。
/dev/shm 是linux下的一块共享内存结构(是TMPFS类型)。默认最大为内存的一半大小.其实它的实际大小可以设置为(物理内存的一半+swap).好多人都把它和swap给混淆了,这是两个不同的概念.但它并不会真正的占用这块内存,如果/dev/shm/下没有任何文件,它占用的内存实际上就是0字节;如果它最大为1G,里头放有100M文件,那剩余的900M仍然可为其它应用程序所使用,但它所占用的100M内存,是绝不会被系统回收重新划分的。
/dev/shm用来存储进程间通讯时的一些共享数据结构..例如Oracle11G的ASMM(Automatic Shared Memory Management)就用/dev/shm进行通讯和数据共享. DBA在进行11G配置时需要考虑MEMORY_MAX_TARGET和MEMORY_TARGET参数不能大于/dev/shm.否则在数据库启动时有可能会产生ORA-00845.
/dev/shm通常在物理内存足够时,会在内存中进行数据交换,如果物理内存缺乏时,会用swap进行数据交换. 支持动态在线调整.在我们需要时可以增加或缩减它的大小.
* Ramfs does not use swap while Tmpfs uses.RAMFS只能使用内存,不能使用swap空间。TMPFS可以使用内存也可以使用swap。Ramfs被限制最多可使用内存大小的一半。
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. RAMFS和TMPFS文件系统上写入的数据,在系统重启/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.推荐使用TMPFS。