Contact Us
Add: 5F, Building 3A, No.8 Xiyuanyi Road, West Lake Science Park, Hangzhou, China
Tel: +86-571-87858811
Mob: +8613616520351
Home > News > Content
How To Release Linux Memory Manually? Take I.mx6 As An Example
Jul 27, 2018

Firstly, the usual situation

First talk "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

among them: 


total memory total, number of memory already used, free number of free memory, the total amount of memory of shared multiple processes, size of disk of buffers Buffer Cache and cached Page Cache -buffers / cache memory: The number of memory of used - buffers - cached + buffers / cache free + buffers + cached available memory = free memory + buffers + cached.

With this foundation, you can know that I now used for 163MB, free for the 86MB, buffer and cached were 10MB, 94MB.

Then let's see if there is any change in 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

After I commanded the implementation of the end, used for 244MB, free for the 4MB, buffers for the 8MB, cached for the 174MB. God, all have been eatten by cached. Do not be nervous, this is to improve the efficiency of file reading practice.

In order to improve disk access efficiency, Linux has done some elaborate design, in addition to the dentry cache (for VFS, speed up conversion ofthe file path name to inode), also took two main Cache way: Buffer

Cache and Page Cache. The former is for disk block read and write, the latter is for the file inode read and write. These Cache are shortened effectively

I / O system calls time(such as read, write, getdents).

Then someone said after a period of time, linux will automatically release the memory used. Wait for some time, 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 seems to be no change. (under the actual situation, the memory management is relevant to Swap also)

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


Secondly, manually release the cache

/ proc is a virtual file system, we can read and write through it as a means of communication with the kernel entity. In other words, you can modify the / proc file to make adjustments to the current kernel behavior. Then we can adjust the / proc / sys / vm / drop_caches to free up memory. The operation is as follows:

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

Manually execute the sync command (Description: sync command to run the sync subroutine. If you must stop the system, run the sync command to ensure file system integrity .sync

Command to write all unwritten system buffers to disk, including modified i-nodes, delayed block I / O, and read-write mapping files)

Quote [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 quote [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, you will find the current used to 66MB, free for the 182MB, buffers for 0MB, cached to 11MB. So it is the effective release of the buffer and cache.

The use of / proc / sys / vm / drop_caches is described below

Quote /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 is a non-destructive operation and dirty objects are not freeable, the user should run sync first.


Thirdly, my opinion

The above article has given a more "intuitive" reply for the Linux memory management questions by a lot of users for many years, I feel a bit like the core development team compromise.

I have reservations about whether I need to use this value or mention the value to the user:


1, it can be seen from the man, this value is provided after 2.6.16 core version,so that is, the old version of the operating system, such as red flag DC 5.0, verison before RHEL 4.x did not have this;

2, if the system memory is enough for the observation, I still intend to see the use of swap and the size of the two values si / so;

User common question is, why free so small, whether memory is not released after close the application?

But in fact, we all know that this is because Linux is different from Windows on the memory management, free small is not that the memory is not enough, you should see the last value of the second line of free:


- / + 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 a memory leak, overflow problems, it can be more quickly to judge from the use of the swap , but the above of free is more difficult to see.

On the contrary, at this time, if we tell the user to modify the system a value, "can" release the memory, free will be bigger. What would the user think? Do not they think the operating system "has a problem"?

So, since the core can quickly clear the buffer or cache, it is not difficult to do (which can be seen from the above operation), but the core did not do it (the default is 0), we should not Just to change it casually.

Under normal circumstances, the application is stably operate on the system, and free value will remain at a stable value, although it may seems smaller.

When there is insufficient memory, the application can not get available memory, OOM error and other issues, we maybe should more to analyze the reasons of the application, such as the amount of users is too large which leads to memory shortage, the application of memory overflow, otherwise, clear the buffer to force to Free up the size of the free, therefore, it may just put the problem to the temporary shield.

So, I think, excluding the lack of memory, unless it is in the software development stage, we need to temporarily clear the buffer to determine the application of memory usage; or application is no longer providing support, even if the application has a problem for the memory , and Can not avoid the case, we could only consider the timing clearing of the buffer. (Unfortunately, such applications usually run on the old operating system version, the above operation can not be resolved also).


  • Newsletter
  • Categories
  • Contact Us
    Add: 5F, Building 3A, No.8 Xiyuanyi Road, West Lake Science Park, Hangzhou, China
    Tel: +86-571-87858811
    Mob: +8613616520351
  • QR Code
  • Copyright © Zhejiang Qiyang Intelligent Technology Co.,Ltd All Rights Reserved.