What Is /dev/shm And Its Practical Usage
来源:互联网 发布:淘宝全球购标准 编辑:程序博客网 时间:2024/05/16 08:31
What Is /dev/shm And Its Practical Usage
refer to: http://www.cyberciti.biz/tips/what-is-devshm-and-its-practical-usage.html
/dev/shm is nothing but implementation of traditional share memory concept. It is an efficient means of passing data between programs. One program will create a memory portion, which other processes(if permitted) can accesss. This will result into speeding up things on Linux.
shm/shmfs is also known as tmpfs, which is a common name for a temporary fie storage facility on manyu Unix-like operationg systems. It is intended to appear as a mounted file system, but one which uses virtual memory instead of a pesistent storage device.
If you type the mount command you will see /dev/shm as a tmpfs file system. Therefore, it is a file system, which keeps all files in virtual memory. Everything in tmpfs is temporary in the sense that no files will be created on your hard drive. If you unmount a tmpfs instance, everything stored therein is lost. By default almost all Linux distros configured to use /dev/shm:
[root@CF_ALY_CN ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 40G 1.6G 36G 5% /
tmpfs 1.0G 0 1.0G 0% /dev/shm
Nevertheless, where can I use /dev/shm?
You can use /dev/shm to improve the performance of application software such as Oracle or overall Linux system performance. On heavily loaded system, It can make tons of difference For example VMware workstation/server can be optimzied to improve your Linux host’s performance(i.e. improve the performance of your virtual machines).
in this example, remount /dev/shm with 8G size as follows:
#mount -o remount,size=8G /dev/shm
To be frank, if you have more than 2G RAM+ multiple Virtual machines, this hack always improves performance. In this example, you will give you tmpfs instance on /disk2/tmpfs which can allocate RAM/SWAP in 5K inodes and it is only accessible by root:
#mount -t tmpfs -o size=5G,nr_inodes=5k,mode=700 tmpfs /disk2/tmpfs
Where
● -o opt1,opt2: Pass various options wish a -o flag followed by a comma separated string of options. In this examples, I used the following options:
n remount: Attemp to remount an already-mounted filesystem. In this example, remount the system and increase its size.
n size=8G or size=5G: Override default maximum size of the /dev/shm filesystem. the size is given in bytes, and rounded up to entire pages. The default is half of the memory, The size parameter also accepts a suffix % to limit this tmpfs instance to that percentage of you phisical RAM: the default, when neither size nor nr_blocks is specified, it size=50%. In this example it is set to 8GiB or 5GiB. The tmpfs mount options for sizing(size, nr_blocks, and nr_inodes) accept a suffix k, m or g for Ki, Mi, Gi(binary kilo, mega and giga) and can be changed on remount.
n nr_inodes=5k: The maximum number of inodes for this instance. The default is half of the number of your phisical RAM pages, or(on a machine with highmem) the number of lowmem RAM pages, whichever is the lower.
n mode=700: Set initial permissions of the root directory.
n tmpfs: Tmpfs is a file system which keeps all fiels in virtual memory.
How do I restric or modify size of /dev/shm permanently?
You need to add or modify entry in /etc/fstab file so that system can read it after the reboot. Edit, /etc/fstab as root user, enter:
#vi /etc/fstab
Append or modify /dev/shm entry as follows to set size to 8G
none /dev/shm tmpfs defaults,size=8G 0 0
Save and close the file. For the changes to take effect immediately remount /dev/shm:
#mount -o remount /dev/shm
Veryfy the same:
#df -h
Recommend readings:
① See man pages of mount regarding tmpfs options.
② /usr/share/doc/kernel-doc-/Documentation/filesystems/tmpfs.txt.
译文:
什么是/dev/shm以及实际的应用
参考: http://www.cyberciti.biz/tips/what-is-devshm-and-its-practical-usage.html
/dev/shm 并不是一个真实的目录但是可以实现传统的共享内存概念,可以有效的在程序间解析数据,一个程序将会创建一个内存区域,如果在允许的情况下其它的程序也可以访问这块内存, 在Linux中就会导致访问的速率变快。
shm/shmfs也被称为tmpfs, tmpfs在许多类Unix系统中有一个通用的名字,临时文件存储设备,一般出现在已挂载的文件系统中,但是也用在虚拟内存中替换真实在设备。
在终端下输入df命令就可以看到/dev/shm被挂载为tmpfs文件系统,因此,它是一个文件夹系统,它让所有的文件都保存的虚拟的内存中,tmpfs中的所有的文件都是临时的,不会在你的硬件上创建,如果你卸载掉分区后,所有的文件将会等的,默认大多数Linux发行版都在使用/dev/shm。
[root@CF_ALY_CN ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 40G 1.6G 36G 5% /
tmpfs 1.0G 0 1.0G 0% /dev/shm
怎样使用/dev/shm?
像Oracle在所有的linux系统上可以使用/dev/shm来提高软件的性能,在重量级的系统负载上,会有不同的差异,如,VMware虚拟机可以优化宿主机的性能(如提高虚拟机的性能).
在下面这个例子中,重新挂载/dev/shm使它的大小变为8G
#mount -o remount,size=8G /dev/shm
如果你的虚拟机有大2G的内存,这将会提高性能,在这个例子中,你将给你的 分区/disk2/tmpfs分配一个5K大小的 inodes并且只有root才可以访问:
#mount -t tmpfs -o size=5G,nr_inodes=5k,mode=700 tmpfs /disk2/tmpfs
各个参数的解释
● -o opt1,opt2: 通过-o flag可以带多个参数,中间使用”,”分隔,下面的例子中使用了下面的几个参数。
n remount: 尝试重新挂载已经挂载的文件系统,在这个例子中,重新挂载并且增加分区的大小.
n size=8G or size=5G: 覆盖 /dev/shm文件系统的默认的最大的容量,并且使用全部的页面,默认是内存的一半,也可以使用内存大小的百分比的形式,默认nr_blocks 是固定的,大小=50%.上面的例子中设置为8G或者是5G,tmpfs可接受的大小单位为 k, m or g for Ki, Mi, Gi(binary kilo, mega and giga)
n nr_inodes=5k: 上面例子中最大的inode的大小为5k,默认是物理内存的一半,
n mode=700: 根目录的初始权限
n tmpfs: 所有的文件都在虚拟内存中
怎样永久的调整或者是修改/dev/shm?
需要修改/etc/fstab 使系统重启后可以正常加载,要使用root用户来修改这个文件
#vi /etc/fstab
none /dev/shm tmpfs defaults,size=8G 0 0
保存,然后运行下面的命令,可立马生效。
#mount -o remount /dev/shm
确定和你分配的是一样的
#df -h
推荐阅读:
① See man pages of mount regarding tmpfs options.
② /usr/share/doc/kernel-doc-/Documentation/filesystems/tmpfs.txt.
- What Is /dev/shm And Its Practical Usage
- What is JSON and Its usage
- What is GSON and Its usage
- RScheduler and its Usage
- auto_ptr and its usage
- What is the difference between /dev/ttyUSB and /dev/ttyACM?
- What is the difference between /dev/ttyUSB and /dev/ttyACM?
- What is the difference between /dev/ttyUSB and /dev/ttyACM
- What is /dev/null
- What is the difference between --save and --save-dev?
- What's NLS_LANG and usage
- /dev/shm
- /dev/shm
- /dev/shm
- /dev/shm
- /dev/shm
- Oracle 11g AMM: MEMORY_TARGET, MEMORY_MAX_TARGET and /dev/shm
- what does router do and its purpose?
- HDOJ 2050 折线分割平面
- 制作一个可以滑动操作的 Table View Cell
- HDU Disney's FastPass(状态压缩DP)
- ExtJS5 - 用鼠标拖拽Grid行进行排序及选择
- JSP EL表达式详细介绍
- What Is /dev/shm And Its Practical Usage
- vs2010无法启动调试
- 数据流重定向中的 '1' , '2' , '&'
- 类别管理及相关连接
- Android之使用 AsyncTask
- Xcode重构功能
- MRC下的setter方法
- SQL Server NonClustered DeadLock
- list tuple dict set小结