技术流ken

运维拯救世界

每天五分钟轻松学运维2019-05-17: Linux清空缓存–技术流ken

查看缓存

 

free -m 命令可以查看内存使用情况

[root@ken ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 777M 755M 1.9M 306M 879M
Swap: 4.0G 261M 3.7G

 

同步数据

 

我们在清理缓存前应该先

sync下

[root@ken ~]# sync
[root@ken ~]# sync

因为系统在操作的过程当中,会把你的操作到的文件资料先保存到buffer中去,因为怕你在操作的过程中因为断电等原因遗失数据,所以在你操作过程中会把文件资料先缓存。

所以我们在清理缓存先要先把buffe中的数据先写入到硬盘中,sync命令

清空缓存

 

[root@ken ~]# echo “3” > /proc/sys/vm/drop_caches

 

上面的echo 3 是清理所有缓存

echo 0 是不释放缓存

echo 1 是释放页缓存

ehco 2 是释放dentries和inodes缓存

echo 3 是释放 1 和 2 中说道的的所有缓存

 

说明:
1>. /proc是一个虚拟文件系统,我们可以通过对它的读写操作作为与kernel实体间进行通信的一种手段。也就是说可以通过修改/proc中的文件,来对当前kernel的行为做出调整。也就是说我们可以通过调整/proc/sys/vm/drop_caches来释放内存。
0 – 不释放
1 – 释放页缓存
2 – 释放dentries和inodes
3 – 释放所有缓存
数字1是用来清空最近放问过的文件页面缓存
数字2是用来清空文件节点缓存和目录项缓存
数字3是用来清空1和2所有内容的缓存。
2>. 关于drop_caches的官方说明如下:
Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to becomefree.
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.
3>. Linux内核会将它最近访问过的文件页面缓存在内存中一段时间,这个文件缓存被称为pagecache。
Inode是linux/unix操作系统中的一种数据结构,包含了各文件相关的一些重要信息。在创建文件系统时,就会同时创建大量的inode。一般inode表会占用文件系统磁盘空间的1%。

目录项缓存(dcache)dentries

各参数含义:
total:总物理内存
used:已使用内存
free:完全未被使用的内存
shared:应用程序共享内存
buffers:缓存,主要用于目录方面,inode值等
cached:缓存,用于已打开的文件
-buffers/cache:应用程序使用的内存大小,used减去缓存值
+buffers/cache:所有可供应用程序使用的内存大小,free加上缓存值
其中:
total = used + free
-buffers/cache=used-buffers-cached,这个是应用程序真实使用的内存大小
+buffers/cache=free+buffers+cached,这个是服务器真实还可利用的内存大小

[oracle@cddserver1 ~]$ free -m
             total       used       free     shared    buffers     cached
Mem:         32096      10379      21717          0         38       7942
-/+ buffers/cache:       2398      29698
Swap:        34287          0      34287
这里面的第二行才是真正服务器的空闲内存
2. Linux的内存分配方式
大家都知道,Linux服务器为了提高效率,会提前申请内存,即使这些内存没有被具体应用使用,Linux也会提前申请这些内存,然后利用这些内存做缓存用,即将刚打开的文件系统存入cache中,这样对应的服务器free值会越来越少,buffers和cached会越来越大,因此给大家表象就是内存越来越少了,大家就紧张了;其实,大家完全不用紧张,Linux服务器在发现内存不足时,会自动清理cached区域,释放内存,然后继续增大cache,free继续减少。因此,那样手动降低内存使用率的方法,其实就是图一时之快。

echo 1 > /proc/sys/vm/drop_caches 可以暂时清空cache和buffer

10 thoughts on “每天五分钟轻松学运维2019-05-17: Linux清空缓存–技术流ken

  1. Aw, this was a really nice post. In thought I want to put in writing like this additionally ?taking time and precise effort to make a very good article?however what can I say?I procrastinate alot and by no means seem to get something done.

  2. My husband and i ended up being really glad when Ervin could carry out his homework using the precious recommendations he got through the weblog. It’s not at all simplistic to simply choose to be giving freely secrets and techniques that many a number of people could have been selling. So we recognize we now have the writer to give thanks to for this. All of the illustrations you’ve made, the simple site navigation, the friendships you can make it easier to instill – it’s most awesome, and it is letting our son and us believe that that content is satisfying, and that is rather essential. Thank you for the whole lot!

  3. I in addition to my friends ended up taking note of the best advice found on your web page and immediately developed an awful feeling I never thanked the web blog owner for those strategies. My guys came thrilled to study all of them and now have in actuality been taking advantage of these things. Appreciation for indeed being considerably considerate as well as for selecting this kind of impressive guides millions of individuals are really eager to be informed on. My very own sincere apologies for not expressing appreciation to you earlier.

  4. My spouse and i felt quite lucky John managed to finish off his investigations through the entire precious recommendations he made using your web page. It is now and again perplexing to simply continually be making a gift of guidelines the rest could have been making money from. And we do know we need the blog owner to give thanks to for that. The specific explanations you made, the easy website menu, the friendships you can make it easier to instill – it’s mostly powerful, and it is aiding our son in addition to us believe that this concept is thrilling, which is certainly truly vital. Many thanks for the whole thing!

  5. I together with my pals happened to be following the nice helpful hints on the blog while the sudden I got a horrible feeling I never thanked the blog owner for them. My boys came as a consequence excited to read through all of them and now have undoubtedly been loving those things. Appreciation for really being considerably considerate and then for using this kind of impressive information millions of individuals are really desperate to be aware of. My very own honest regret for not expressing gratitude to you earlier.

  6. I wanted to type a simple remark to be able to express gratitude to you for all of the great ways you are showing at this website. My incredibly long internet search has at the end of the day been recognized with awesome strategies to write about with my neighbours. I would assert that we readers actually are unquestionably blessed to be in a notable website with very many special individuals with helpful hints. I feel very much blessed to have encountered your site and look forward to really more brilliant minutes reading here. Thanks a lot once more for a lot of things.

  7. I want to convey my gratitude for your generosity giving support to those people that require help on this particular niche. Your special dedication to getting the solution throughout had been unbelievably beneficial and have consistently allowed those like me to get to their goals. Your own helpful information indicates a lot a person like me and a whole lot more to my office workers. Thanks a ton; from all of us.

  8. I would like to voice my respect for your kind-heartedness for individuals that should have help on this one topic. Your very own commitment to getting the solution across has been especially helpful and has in most cases made men and women much like me to get to their endeavors. Your entire invaluable instruction can mean a lot a person like me and additionally to my colleagues. Warm regards; from everyone of us.

  9. I intended to send you that very small word so as to thank you very much over again on the striking solutions you’ve provided on this page. It has been quite tremendously generous of people like you to provide extensively precisely what most of us would have advertised for an e-book to get some money for their own end, and in particular now that you could possibly have tried it in case you considered necessary. These good ideas as well acted to be a fantastic way to be sure that some people have the identical interest similar to my very own to know a whole lot more around this issue. I believe there are thousands of more enjoyable sessions up front for individuals who looked over your site.

  10. Thanks so much for giving everyone such a splendid chance to check tips from here. It really is very ideal and as well , jam-packed with amusement for me and my office mates to visit your website the equivalent of thrice a week to find out the newest secrets you have. Not to mention, I am just actually satisfied concerning the spectacular opinions served by you. Certain 4 tips on this page are without a doubt the most suitable we have all had.

curry 7进行回复 取消回复

电子邮件地址不会被公开。