Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manually free linux memory

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to manually release linux memory, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Manually free linux memory-/ proc/sys/vm/drop_cache

Memory view of linux:

[root@localhost 0.1.0] # free-m

Total used free shared buffers cached

Mem: 4032 694 3337 0 0 25

It should be noted that the used=free+buffers+cached of mem, in some cases, cached takes up a lot of resources, but the value is not correct. In fact, it does not affect the actual use. The following reprint section explains how to remove the occupation of cached (in fact, it can not be removed and will not affect the actual use).

When files are accessed frequently under Linux, the physical memory will soon be used up. When the program is finished, the memory will not be released normally, but will always be used as a caching. It seems that many people are asking this question, but they do not see any good solution. So let me talk about 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 0 511

Where:

Total memory for reference total

Number of memory already used by used

Number of free memory in free

Total memory shared by multiple processes in shared

Size of buffers Buffer Cache and cached Page Cache disk caches

-memory of buffers/cache: used-buffers-cached

Memory of + buffers/cache: 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: 249 244 4 0 8 174

-/ + buffers/cache: 62 187

Swap: 511 0 511

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.

[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. (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:

[root@server test] # cat / proc/sys/vm/drop_caches

0

First, the value of / proc/sys/vm/drop_caches defaults to 0.

[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)

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

[root@server test] # cat / proc/sys/vm/drop_caches

three

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

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 / proc/sys/vm/drop_caches is explained below

Reference / 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:

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 last value in the second line of free: "- / + buffers/cache: 58191". 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 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.

test

[root@testserver] # uname-a

Linux testserver 2.6.18-164.el5 # 1 SMP Thu Sep 3 03:28:30 EDT 2009 x86 "64 GNU/Linux

[root@testserver] # free-m

Total used free shared buffers cached

Mem: 2013 1661 352 0 223 1206

-/ + buffers/cache: 231 1782

Swap: 2047 0 2047

[root@testserver ~] # sync

[root@testserver ~] # sync

[root@testserver ~] # cat / proc/sys/vm/drop_caches

0

[root@testserver ~] # echo 3 > / proc/sys/vm/drop_caches

[root@testserver ~] # cat / proc/sys/vm/drop_caches

three

[root@testserver] # free-m

Total used free shared buffers cached

Mem: 2013 100 1913 00 14

-/ + buffers/cache: 85 1927

Swap: 2047 0 2047

[root@testserver ~] #

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report