Knowledge about memory release in Linux

Source: Internet
Author: User

Careful friends will notice that when you frequently access files in Linux, the physical memory will soon be used up. When the program ends, the memory will not be released normally, but will always be used as caching. it seems that many people are asking this question, but they have not seen any good solutions. let me talk about this.

Let's talk about the free command first.

[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

Where:

Total memory

Used memory used

Free idle memory

Total memory shared by multiple processes

Buffers buffer cache and cached page cache disk cache size

-Buffers/cache memory: Used-Buffers-cached

+ Buffers/cache memory: Free + buffers + cached

Available memory = free memory + buffers + cached

With this foundation, we can know that I now used is 163 MB, free is 86, buffer and cached are respectively 10, 94

Let's take a look at the memory changes if I copy the 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

After I run the command, used is 244 MB, free is 4 MB, buffers is 8 MB, and cached is 174 MB. don't be nervous. This is to improve the efficiency of File Reading.

Reference http://www.wujianrong.com/archives/2007/09/linux_free.html "in order to improve disk access efficiency, Linux has made some careful design, in addition to the dentry cache (for VFS, to accelerate the conversion of file path to inode ), two major cache methods are also adopted: buffer cache and page cache. The former is used to read and write disk blocks, and the latter is used to read and write inode files. These caches effectively shorten the time for I/O system calls (such as read, write, getdents. "

So some people have said that Linux will automatically release the memory used for a while. Let's try again with free to see if there is any 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

Ms remains unchanged. Can I manually release the memory ??? The answer is yes!

/Proc is a virtual file system. We can use its read/write operations as a means to communicate with the kernel object. in other words, you can modify the file in/proc to adjust the current kernel behavior. then we can release the memory by adjusting/proc/sys/Vm/drop_caches. the procedure is as follows:

[Root @ server test] # Cat/proc/sys/Vm/drop_caches
0
First, the value of/proc/sys/Vm/drop_caches. The default value is 0.

[Root @ server test] # Sync

Run the sync command manually (Description: The 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 unwritten system buffers to the disk, including modified I-nodes, delayed block I/O, and read/write ing files)

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

Set/proc/sys/Vm/drop_caches 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

Run the free command and find that the current used is 66 MB, free is 182 MB, buffers is 0 MB, and cached is 11 mb. This effectively releases the 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;
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 shoshould run sync (8) first.

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.