In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to manually release linux memory, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
I. under normal circumstances
Let's start with the free command:
Quote
[root@server] # free-m total used free shared buffers cached Mem: 249 163 86 0 10 94-/ + buffers/cache: 58 191 Swap: 511 0511
Where:
Quote
Total total memory Total 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 now have 163MB of used, 86MB of free buffer and 10MB of cached of 94MB.
So let's see what happens to memory if I copy the file.
Quote
[root@server] # cp-r / etc ~ / test/ [root@server ~] # free-m total used free shared buffers cached Mem: 249244 408 174-/ + buffers/cache: 62 187 Swap: 511 0511
After the execution of my command, the used was 244MB, free, 4MB, buffers, 8MB, cached, 174MB. Oh, my God, it was 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 designs, in addition to caching dentry (for VFS to speed up the conversion of file pathnames to inode), but also adopts two main Cache methods: Buffer Cache and Page Cache. The former is for the read and write of disk blocks, and the latter is for the read and write of file inode. These Cache effectively shorten the time it takes to make system calls such as read,write,getdents.
So it has been said that for a period of time, linux will automatically release the memory used. After waiting for a while, let's try again with free to see if there is a release.
Quote
[root@server test] # free-m total used free shared buffers cached Mem: 249 244 50 08 174-/ + buffers/cache: 61 188 Swap: 511 0511
There seems to be no change. (in practice, memory management is also related to Swap.)
So can I release this memory manually? The answer is yes!
Release the cache manually
/ proc is a virtual file system, and we can read and write to it as a means of communicating with kernel entities. That is, you can adjust the behavior of the current kernel by modifying the file in / proc. Then we can free up memory by adjusting / proc/sys/vm/drop_caches. Do the following:
First of all, refer to [root@server test] # cat / proc/sys/vm/drop_caches 0, the value of / proc/sys/vm/drop_caches. The default is 0. Quote [root@server test] # sync
Execute the sync command manually (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 unwritten system buffers to disk, including the modified i-node, deferred block Imax O, and read-write mapping file)
Reference [root@server test] # echo 3 > / proc/sys/vm/drop_caches [root@server test] # cat / proc/sys/vm/drop_caches 3 to set the / proc/sys/vm/drop_caches value to 3 reference [root@server test] # free-m total used free shared buffers cached Mem: 249 66 182 00 11-/ + buffers/cache: 55 194 Swap: 511 0511
If you run the free command, you will find that the current used is 66MB, free, 182MB, buffers, 0MB, cached, 11MB. So effectively released buffer and cache.
The usage of ◎ related to / proc/sys/vm/drop_caches is explained 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.
Third, my opinion
The above article gives a more "intuitive" response to many users' long-standing questions about Linux memory management, which is more like a compromise from the core development team.
I have reservations about whether or not to use this value, or to mention it to the user:
Quote
1. As you can see from man, this value is only available in core versions after 2.6.16, that is, older operating systems, such as Red Flag DC 5.0 and RHEL 4.x, are not available.
2. If I observe whether the system memory is enough, I still intend to look at the utilization rate of swap and the size of the two values of si/so.
The common question for users is why the free is so small and whether the memory is not freed after closing the application.
But in fact, we all know that this is because Linux manages memory differently from Windows. A small free does not mean that there is not enough memory. You should look at the second line of free * a value:
Quote
-/ + buffers/cache: 58 191
This is the amount of memory available to the system.
The actual project tells us that if there are problems such as memory leaks or overflows in the application, it can be judged quickly from the usage of swap, but it is more difficult to check on free.
On the contrary, if at this time, we tell the user to modify a value of the system, "can" free memory, the free will be large. What will users think? Don't you think there's something wrong with the operating system?
So, I think since the core can quickly empty buffer or cache, it is not difficult to do so (as you can clearly see from the above operation), but the core does not do so (the default is 0), we should not change it casually.
In general, when the application runs steadily on the system, the free value will remain at a stable value, although it may seem small.
When there are problems such as insufficient memory, lack of available memory for applications, and OOM errors, it is more important to analyze the reasons for applications, such as insufficient memory caused by too many users, application memory overflow, etc. Otherwise, clearing buffer and forcing the size of free may only block the problem temporarily.
In my opinion, except in the case of insufficient memory, unless the buffer needs to be temporarily cleared during the software development phase to determine the memory usage of the application, or the application no longer provides support, even if the application does have problems with memory and is unavoidable, consider emptying the buffer regularly. Unfortunately, such applications usually run on older versions of the operating system, and the above operations can't be solved.
The above is all the contents of the article "how to release linux memory manually". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.