There are always a lot of friends in the Linux memory management in question, the previous Linux memory management method does not seem to clear everyone's doubts. And in the new version of the core, it seems to provide a new solution to this problem, special turn out for you to refer to. Finally, I enclose my views on this approach and welcome you to discuss it together.
When the files are frequently accessed under Linux, physical memory is quickly used up, and when the program is finished, memory is not released normally, but it is always caching. This problem, seems to have a lot of people are asking, but did not see what is a very good solution. So let me talk about this.
I. Usual SITUATION
Let's say free command:
# free-m
Total used free shared buffers Cached
mem:249 163 86 0 10 94
-/+ buffers/cache:58 191
swap:511 0 511
which
Total Memory
used the number of memory already in use
Free amount of memory
Total memory shared by multiple processes
Buffers buffer cache and cached Page cache disk size
-buffers/cache (used) Memory: used-buffers-cached
Number of +buffers/cache (available): Free + buffers + Cached
Available Memory=free memory+buffers+cached
With this foundation, it can be learned that I now used for 163mb,free for 86mb,buffer and cached respectively for 10MB,94MB.
So let's see what happens to memory if I execute the copy file.
# cp-r/etc ~/test/
# free-m
Total used free shared buffers Cached
mem:249 244 4 0 8 174
-/+ buffers/cache:62 187
swap:511 0 511
At the end of my command, used for 244mb,free for the 4mb,buffers for 8mb,cached 174MB, God, have been cached eaten. Don't be nervous, this is to improve the efficiency of the file reading practice.
In order to improve disk access efficiency, Linux has done a number of careful design, in addition to the Dentry cache (for VFS, speed file path name to the Inode conversion), but also took two main Cache: Buffer cache and Page cache. The former is read and write to disk block, the latter is read and write to the file inode. These cache effectively shorten the time of the I/O system call (such as read,write,getdents).
So some people say that Linux automatically frees up memory for a period of time. After waiting for a while, we use free to try again to see if there is release.
# free-m
Total used free shared buffers Cached
mem:249 244 5 0 8 174
-/+ buffers/cache:61 188
swap:511 0 511
There seems to be no change. (In fact, memory management is also related to swap) so I can manually release these memory. The answer is OK.
Ii. manual release of the cache
/proc is a virtual file system that we can use as a means of communicating with kernel entities through its read and write operations. That is, you can make adjustments to the current kernel behavior by modifying the files in/proc. Then we can release the memory by adjusting the/proc/sys/vm/drop_caches. The operation is as follows:
# cat/proc/sys/vm/drop_caches
0
First, the/proc/sys/vm/drop_caches value defaults to 0.
# Sync
Manually perform the sync command (description: The sync command runs the Sync subroutine.) If the system must be stopped, run the sync command to ensure the integrity of the file system. The Sync command writes all of the unused system buffers to disk, including modified I-node, deferred block I/O, and read-write mapping files.
# echo 3 >/proc/sys/vm/drop_caches
# cat/proc/sys/vm/drop_caches
3
Set the/proc/sys/vm/drop_caches value to 3
# free-m
Total used free shared buffers Cached
mem:249 66 182 0 0 11
-/+ buffers/cache:55 194
swap:511 0 511
To run the free command, you will find that the current used is 66mb,free to 182mb,buffers 0mb,cached to 11MB. So effective release of buffer and cache.
The usage of/proc/sys/vm/drop_caches is described below
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to become Free.
To-free Pagecache, use echo 1 >/proc/sys/vm/drop_caches;
To free dentries and inodes, use echo 2 >/proc/sys/vm/drop_caches;
To free Pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches.
Because this are a non-destructive operation and dirty objects are not freeable, the user should run sync a.
Third, my opinion
This article has long been a lot of users of the Linux memory management questions, gives a more "intuitive" response, I feel a bit like the core development team compromise. For the need to use this value, or to mention this value to the user, I have reservations: from the man can see that the value from the core version of 2.6.16 to provide, that is, the old version of the operating system, such as the Red Flag DC 5.0, RHEL 4.x before the version is not, if the system memory is sufficient to observe , I still want to see the use of swap and the size of the si/so two values;
The common question for users is why the free is so small that the memory is not released after the application is turned off. But in fact, we all know that this is because Linux management of memory is different from windows, free small does not mean that memory is not enough, should be seen in the second line of the last value:-/+ buffers/cache:58 191, this is the system available memory size.
The actual project tells us, if because of the application has like memory leaks, overflow problem, the use of swap can be faster to judge, but free above it is more difficult to see. Conversely, if at this time, we tell the user to modify a value of the system, "can" release the memory, Free is big. What users will think. Don't think the operating system is "problematic". So, I think since the core is able to quickly empty the buffer or cache, it is not difficult to do (this is evident from the operation above), but the core did not do so (the default value is 0), we should not change it casually. Generally, the application runs stably on the system, and the free value remains in a stable value, although it may look smaller.
When there is not enough memory, the application gets no available memory, oom errors, or more should be analyzed application reasons, such as the user is too large to cause insufficient memory, the application of memory overflow, etc., otherwise, empty the buffer, forced to free the size, may just put the problem to temporarily shield.
I think that out of memory out of the case, unless the software development phase, the need to temporarily clear the buffer to determine the application of memory usage, or application has no longer provide support, even if the application of memory when there is a problem, and can not be avoided, only consider timing empty buffer. (Unfortunately, such applications are usually run on older versions of the operating system and cannot be solved by the above operations.) and the server in the production environment can not consider manual free memory, this will bring more problems. Remember that memory is for use, not for a look. Not like Windows.
No matter how much your real physical memory is, he will have to use the hard disk to exchange files to read. This is why Windows often prompts for the lack of virtual space, you think how boring, in memory and most of the time, take out a part of the hard disk space to act as memory. How can the hard disk faster than memory, so we look at Linux, as long as the swap space without swap, we do not have to worry about their own memory too little. If you use a lot of swap, you might want to consider adding physical memory, which is the standard for Linux to see if the memory is enough. Of course, this only represents my personal opinion, but also welcome everyone to exchange discussions.
The above content is reproduced in the examination big, below is I write a memory release script, shares to everybody:
# vim/root/satools/freemem.sh
#!/bin/bash
used= ' free-m | awk ' nr==2 ' | awk ' {print $} '
free= ' free-m | awk ' nr==2 ' | awk ' {print $} '
EC Ho "===========================" >>/var/log/mem.log
date >>/var/log/mem.log
echo Memory Usage | [USE:${USED}MB] [FREE:${FREE}MB] ">>/var/log/mem.log
if [$free-le]; then
sync && echo 1 >/proc/sys/vm/d Rop_caches
sync && echo 2 >/proc/sys/vm/drop_caches
sync && echo 3 >/proc/sys/vm/drop_ Caches
echo "OK" >>/var/log/mem.log
else
echo "not required" >>/var/log/mem.log
Add the script to the Crond task, timed to execute.
# echo "*/30 * * * root/root/satools/freemem.sh" >>/etc/crondtab