Release cache memory under Linux __linux

Source: Internet
Author: User
release cache memory under LinuxCareful friends will notice that when you frequently access files under Linux, physical memory will soon be used up, and when the program is finished, memory will not be released normally, but as a caching. This question seems to be a lot of people asking, But I don't see any good solutions. Then let me talk about this.

Let's say free command.

[Root@server ~]# 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

Number of-buffers/cache Memory: used-buffers-cached

+buffers/cache Memory: Free + buffers + Cached

Available Memory=free memory+buffers+cached

With this foundation, it can be learned that I now used for 163mb,free for 86,buffer and cached respectively for 10,94

So let's see what happens to memory if I execute the copy file.

[Root@server ~]# cp-r/etc ~/test/
[Root@server ~]# 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 8mb,cached for 174MB, the days are cached eaten. Don't be nervous, this is to improve the efficiency of the file reading practice.

reference [Url]http://www.2qyou.com/thread-591-1-1.html[/url] to improve disk access efficiency, Linux has done some careful design, in addition to caching dentry (for VFS, Accelerates file path name to Inode conversion), also takes two main cache methods: 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 someone said that some time, Linux will automatically release the memory used, we use free to try again to see if there is release?

[Root@server 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 the memory??? The answer is OK!

/proc is a virtual file system that we can use to communicate 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 adjust the/PROC/SYS/VM /drop_caches to free memory. The operation is as follows:

[Root@server test]# Cat/proc/sys/vm/drop_caches
0
First, the/proc/sys/vm/drop_caches value defaults to 0.

[Root@server test]# 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.

[Root@server test]# echo 3 >/proc/sys/vm/drop_caches
[Root@server test]# Cat/proc/sys/vm/drop_caches
3

Set the/proc/sys/vm/drop_caches value to 3

[Root@server test]# Free-m
Total used free shared buffers Cached
mem:249 66 182 0 0 11
-/+ buffers/cache:55 194
swap:511 0 511

Then run the free command and find that the used is now 66mb,free for 182mb,buffers 0mb,cached 11MB. So effectively released 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 which 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
Are not freeable, the user should run Sync (8).

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.