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 free cache memory under linux

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

Share

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

This article introduces the knowledge of "how to release cache memory under linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Careful friends will notice that when you access files 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 caching. It'seems that many people are asking this question, but they don't see any good solution. Then let me talk about it.

Let's start with the 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

Where:

Total total memory

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, we can know that I now have a used of 163MB, a free buffer of 86MB, and a cached of 1094m.

So let's see what happens to memory 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 the execution of my command, the used was 244MB, free, 4MB, buffers, 8MB, cached, 174MB. Oh, my God, it was eaten by cached. Don't worry, this is to improve the efficiency of file reading.

Reference [url] http://www.2qyou.com/thread-591-1-1.html[/url] in order to improve disk access efficiency, 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 someone said that for a while, linux will automatically release the memory used. Let's use free to try again 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

There is no change in MS, so can I release the memory manually? The answer is yes!

/ proc is a virtual file system, and we can read and write it as a means to communicate with kernel entities. In other words, the current kernel behavior can be adjusted 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, which 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

Then run the free command, and 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

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

# echo 1 > / proc/sys/vm/drop_caches

# echo 2 > / proc/sys/vm/drop_caches

# echo 3 > / proc/sys/vm/drop_caches.

Because this is a non-destructive operation and dirty objects

Add: the top command holds down M, which is about the sorting of memory from the largest to the smallest, and pressing P is the sort of occupancy of CPU. The pmap process number shows the memory occupied by the process. PS X | GREP process number also has this effect. Vmstat shows the utilization of system resources and memory.

This is the end of "how to free cache memory under linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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