How to manually release linux memory

  

There are always many friends who have doubts about Linux memory management. In the new version of the core, it seems to provide a new solution to this problem, especially for the reference. Finally, I also attached my comments on this method, and I welcome everyone to discuss it together. When the file is accessed frequently under Linux, the physical memory will be used up quickly. When the program ends, the memory will not be released normally, but will always be caching. This question seems to be a lot of people asking, but they have not seen any good solution. Then let me talk about this issue.

First, usually the first free command: reference [root@server ~]# free -mtotal used free shared buffers cachedMem: 249 163 86 0 10 94-/+ buffers/cache: 58 191Swap: 511 0 511

Where: Total total memory referenced used memory used free free memory shared 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, you can know that I am now 163MB, free is 86MB, buffer and cached are 10MB, 94MB. So let's see if there is any change in memory if I execute the copy file. Reference [root@server ~]# cp -r /etc ~/test/[root@server ~]# free -mtotal used free shared buffers cachedMem : 249 244 4 0 8 174-/+ buffers/cache: 62 187Swap: 511 0 511

After the execution of my command, used is 244MB, free is 4MB, buffers is 8MB, cached is 174MB, day Oh, they are all eaten by cached. Don't worry, this is to improve the efficiency of file reading.

In order to improve the efficiency of disk access, Linux has made some careful design, in addition to caching dentry (for VFS, speed up file path name to inode conversion), also adopted two main Cache methods: BufferCache and PageCache. The former is for reading and writing of disk blocks, and the latter is for reading and writing of file inodes. These Caches effectively reduce the time of I/O system calls (such as read, write, getdents).

So some people say that for a while, Linux will automatically release the memory used. After waiting for a while, let's try again with free and see if there is a release? Reference [root@server test]# free -mtotal used free shared buffers cachedMem: 249 244 5 0 8 174-/+ buffers/cache: 61 188Swap: 511 0 511

It seems that there is no change. (In fact, memory management is also related to Swap)

So can I manually release this memory? The answer is yes!

Second, manual release cache /proc is a virtual file system, we can read and write operations as a means of communication with the kernel entity. In other words, you can adjust the behavior of the current kernel by modifying the files in /proc. Then we can free up memory by adjusting /proc/sys/vm/drop_caches. The operation is as follows: Reference [root@server test]# cat /proc/sys/vm/drop_caches0

First, the value of /proc/sys/vm/drop_caches defaults to 0. Reference [root@server test]# sync

Manually execute the sync command (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 will All unwritten system buffers are written to disk, containing modified i-nodes, delayed block I/O, and read-write mapping files) reference [root@server test]# echo 3 > /proc/sys/Vm/drop_caches[root@server test]# cat /proc/sys/vm/drop_caches3

Set the /proc/sys/vm/drop_caches value to 3 reference [root@server test]# free -mtotal used Free shared buffers cachedMem: 249 66 182 0 0 11-/+ buffers/cache: 55 194Swap: 511 0 511

Run the free command again, you will find that now used is 66MB, free is 182MB, buffers is 0MB The cached is 11MB. So effectively release 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; tofree 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 objectsare not freeable, the user Should run sync first.

Three, my opinion The above article has long been a question of many users on Linux memory management, given a comparison "intuitive" response, I feel a bit like Compromise by the core development team. I have reservations about whether I need to use this value, or to mention this value to the user: Reference 1, from man, you can see that this value is only available from the core version after 2.6.16, which is the old version of < u>Operating system
, such as Red Flag DC5.0, RHEL 4.x before the version is not; 2, if the system memory is enough to observe, I still intend to see the swap usage and si /so two The size of the value;

The common question for users is, why is free so small, is the memory not released after closing the application? But in fact, we all know that this is because Linux's memory management is different from Windows
, free small does not mean that the memory is not enough, it should be the last value of the second line of free: reference - /+ buffers/cache: 58 191

This is the amount of memory available to the system. The actual project tells us that if the application has problems like memory leaks and overflows, the usage from swap can be judged relatively quickly, but the above is more difficult to view. On the contrary, if at this time, we tell the user to modify a value of the system, & quoquo; can & quoquo; free memory, free is big. What do users think? Will not feel that the operating system & rdquo; have problems & rdquo;? So, I think that since the core is able to quickly clear the buffer or cache, it is not difficult to do (this is obvious from the above operation), but the core does not do this (the default value is 0), we should not Just change it.

Copyright © Windows knowledge All Rights Reserved