/dev/shm/under Linux and the difference from the swap directory

Source: Internet
Author: User
Tags tmp folder volatile

Excerpt from: http://miaozk2006.blog.163.com/blog/static/3824705820111028183996/

First,/dev/shm/Introduction:

/dev/shm/is a very useful directory for Linux because it is not on the hard disk, but in memory. Therefore, under Linux, it is not necessary to build RAMDisk, the direct use of/dev/shm/can achieve a good optimization effect.
One of the/dev/shm/needs to be aware of is the capacity issue, which, under Linux, defaults to half the size of memory and can be seen using the df-h command. But it does not really occupy this memory, if there is no file under the/dev/shm/, it occupies the memory is actually 0 bytes, if it is a maximum of 1G, there are 100M files inside, the remaining 900M can still be used for other applications, but it occupies the 100M memory, Will never be re-divided by the system recycling, or who dares to deposit documents inside?
The default maximum half of the memory size may not be sufficient in some cases, and the default inode count is generally higher, so you can use the Mount command to manage it. Mount-o size=1500m-o nr_inodes=1000000-o Noatime,nodiratime-o REMOUNT/DEV/SHM


On a 2G machine, the maximum capacity is tuned to 1.5G, and the inode number is set to 1000000, which means roughly 1 million small files can be deposited.

You can occupy swap space when the/DEV/SHM space is not enough, so don't worry about storage space. Think about how fast PHP reads and writes sessions from disk IO operations to memory operations. Just be aware that the data stored in the/DEV/SHM will be lost after the server restarts.

The default system will load/DEV/SHM, which is called TMPFS, some say with RAMDisk (virtual disk), but not the same. Like a virtual disk, TMPFS can use your RAM, but it can also be stored using your swap partition. And the traditional virtual disk is a block device, and requires a command such as MKFS to really use it, TMPFS is a file system, not a block device; you just install it and it's ready to use.
TMPFS has the following advantages:
1, the size of the dynamic file system.
Another major benefit of 2,TMPFS is its lightning speed. Because a typical TMPFS file system resides completely in RAM, read-write can be almost instantaneous.
3,TMPFS data is not retained after a reboot because virtual memory is inherently volatile. So it is necessary to do some scripts such as loading, binding operations.
All right, let's talk about the application:
First, create a TMP folder in/dev/stm and then bind to the actual/tmp
Mkdir/dev/shm/tmp
chmod 1777/dev/shm/tmp
Mount--bind/dev/shm/tmp/tmp (--bind)
When you use the Mount--bind olderdir newerdir command to mount a directory to another directory, all information such as the permissions and owners of the newerdir changes. The mounted directory inherits all the properties of the mounted directory, except for the name.
Using TMPFS to speed up your Linux server is to use virtual disks to store squid cache files and PHP seesion. It's a lot faster!
The default system will load/DEV/SHM, which is called TMPFS, some say with RAMDisk (virtual disk), but not the same. Like a virtual disk, TMPFS can use your RAM, but it can also be stored using your swap partition. And the traditional virtual disk is a block device, and requires a command such as MKFS to really use it, TMPFS is a file system, not a block device; you just install it and it's ready to use.
TMPFS has the following advantages:
1. The size of the dynamic file system,
2. Another major benefit of TMPFS is its lightning speed. Because a typical TMPFS file system resides completely in RAM, read-write can be almost instantaneous.
3. TMPFS data is not retained after a reboot because virtual memory is inherently volatile. So it is necessary to do some scripts such as loading, binding operations.
All right, let's talk about some great principles, let's get bored, or tell me about my application:
First, create a TMP folder in/dev/shm and then bind to the actual/tmp
Mkdir/dev/shm/tmp
chmod 1777/dev/shm/tmp
Mount--bind/dev/shm/tmp/tmp
1. Squid Cache directory settings
Vi/etc/squid/squid.conf
Modified into
Cache_dir ufs/tmp 256 16 256
The first 256 here means using 256M of memory, and I think the method of using RAMDisk in http://www.linuxaid.com.cn/articles/4/4/441672019.shtml is not as good as using TMPFS directly, At least every time you start without mkfs, you can change the size dynamically. At this time,/tmp is actually/dev/shm/tmp
Then restart the service, OK, now all the squid cache files are saved in the Tmpfs file system, soon oh.
2. Optimization of PHP performance
For a large access to the apache+php site, there may be a lot of temporary files under TMP, such as seesion or some cache files, then you can save it to the Tmpfs file.
Save Seesion method is very simple as long as modify php.ini, because I have/dev/stm/tmp and/tmp binding, so do not overwrite also line, as for the PHP program generated cache file that can only change their own PHP program:)
As for other applications of TMPFS, I think we may be inspired by this article.

Second, the difference with swap:
/dev/shm
The first thing you can see is that/DEV/SHM is a device file, you can think of/DEV/SHM as a system memory entry, you can think of it as a physical storage device,
A TMP filesystem that allows you to read and write files to memory through this device to speed up certain I/O operations, such as open, write, read, and more frequently on a large file.
It is said that Oracle is using/DEV/SHM (Shitou has not used Oracle) and can list the current/DEV/SHM mounted file system via the Mount command.
You can read and write directly to the/DEV/SHM, for example:
#touch/dev/shm/file1
Since it is a memory-based file system, the files under/dev/shm will not exist after the system restarts.

The size of the Linux default (CentOS)/DEV/SHM partition is 50% of the system's physical memory, although the use of/DEV/SHM is much more efficient for file operations.
However, it is seldom used in the current release software (except for Oracle mentioned earlier) and can be viewed through LS/DEV/SHM to see if there are any files below, and if not, the current system does not use the device.

Swap
And swap is the virtual memory of Linux, that is, to open up space on the hard disk when memory is not enough to use as memory, it can be understood as when the/dev/shm space is not enough to occupy swap space

Excerpt from: http://hi.baidu.com/heyuejuan/blog/item/e7aba945ba27c2288694730a.html

/dev/shm/under Linux and the difference from the swap directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.