Transferred from: http://www.linuxidc.com/Linux/2015-02/112685.htm
after the file is frequently accessed under Linux, the physical memory is quickly exhausted, and when the program is finished, the memory is not released normally, but it is always used as a caching. This problem seems to be a lot of people are asking, but did not see any good solution. then I'll talk about it.
First, the free command.
[Email protected] ~]# 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 Totals
Used number of memory already in use
Free amount of memory
Shared memory totals for multiple processes
Buffers buffer cache and cached Page cache disk size
-buffers/cache Number of Memory: used-buffers-cached
+buffers/cache Number of Memory: Free + buffers + Cached
Available Memory=free memory+buffers+cached
With this foundation, you can be informed that I now used for 163mb,free for 86,buffer and cached respectively for 10,94
So let's take a look at what happens to memory if I perform a copy of the file.
[Email protected] ~]# cp-r/etc ~/test/
[Email protected] ~]# free-m
Total used free shared buffers Cached
mem:249 244 4 0 8 174
-/+ buffers/cache:62 187
swap:511 0 511
After the completion of the Linux command, used for the 244mb,free 4mb,buffers for the 8mb,cached for the 174MB, God is eaten by cached. Relax, this is a way to improve the efficiency of file reading.
In order to improve disk access efficiency, Linux commands have been carefully designed, in addition to caching dentry (for VFS, accelerating file path name to Inode conversion), there are two main cache modes: Buffer cache and Page cache. The former is for the disk block read and write, the latter for the file inode read and write. These caches effectively shorten the time for I/O system calls (such as read,write,getdents). “
Then someone said that for a while, Linux will automatically release the memory used, we use free to try again to see if there is a release "?
[Email protected] test]# free-m
Total used free shared buffers Cached
mem:249 244 5 0 8 174
-/+ buffers/cache:61 188
swap:511 0 511
There is no change in MS, so can I manually release these memory??? The answer is YES!
/proc is a virtual file system, which can be used as a means of communicating with kernel entities through its read and write operations. This means that you can make adjustments to the current kernel behavior by modifying the files in the/proc. Then we can release the memory by adjusting the/proc/sys/vm/drop_caches. The operation is as follows:
[Email protected] test]# cat/proc/sys/vm/drop_caches
0
First, the value of/proc/sys/vm/drop_caches, which defaults to 0
[[Email protected] test]# Sync
Perform the sync command manually (description: Sync command runs the Sync subroutine. If you must stop the system, run the Sync command to ensure the integrity of the file system. The Sync command writes all the non-writable system buffers to disk, including modified I-node, deferred block I/O, and read-write mapping files.
[Email protected] test]# echo 3 "/proc/sys/vm/drop_caches
[Email protected] test]# cat/proc/sys/vm/drop_caches
3
Set the/proc/sys/vm/drop_caches value to 3
[Email protected] test]# free-m
Total used free shared buffers Cached
mem:249 66 182 0 0 11
-/+ buffers/cache:55 194
swap:511 0 511
Run the free command again and find that the current used is 66mb,free to 182mb,buffers for 0mb,cached to 11MB. The buffer and cache are released effectively.
The usage of/proc/sys/vm/drop_caches is explained 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 the 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 is a non-destructive operation and dirty objects
How to release cache memory under Linux