首页 技术 正文
技术 2022年11月12日
0 收藏 419 点赞 3,610 浏览 1409 个字

转自:https://www.cnblogs.com/madsnotes/articles/5740495.html

频繁的文件访问会导致系统的Cache使用量大增。例如:在使用grep从很多文件中搜索特定数据串的时候,发现内存使用迅速提高,主要是cache的使用占用了相当多的内存。在使用下面命令的时候忽视了文件的数量和文件大小,导致cache突增。

# grep -e "dst_string" ./*  

  大家在使用shell编程的时候一定要注意通配符的使用,这里尤其提醒大家就是星号(*)的使用,星号固然方便,但要适度使用。  这里主要还是记录如何手动清理linux内存cache,因为上面的操作使用的大量的cache。

Linux之清理linux内存cache

# 使用free查看当前系统内存使用情况
[root@bogon ~]# free
total used free shared buffers cached
Mem: 1938784 89628 1849156 248 60 17644
-/+ buffers/cache: 71924 1866860
Swap: 2097148 0 2097148# 执行grep操作
[root@bogon ~]# grep -e "dsf" ./*# 再次查看内存使用情况
[root@bogon ~]# free
total used free shared buffers cached
Mem: 1938784 100020 1838764 244 3608 26020
-/+ buffers/cache: 70392 1868392
Swap: 2097148 0 2097148

Linux之清理linux内存cache

下面介绍如何清理cached

Linux之清理linux内存cache

# 执行sync同步数据,防止数据或操作丢失(重要),将未写的系统缓冲区写到磁盘中。包含已修改的 i-node、已延迟的块 I/O 和读写映射文件
sync
# 清理cached
[root@bogon ~]# echo 3 > /proc/sys/vm/drop_caches # 查看清理cached后的内存使用情况
[root@bogon ~]# free
total used free shared buffers cached
Mem: 1938784 87116 1851668 240 188 17596
-/+ buffers/cache: 69332 1869452
Swap: 2097148 0 2097148

Linux之清理linux内存cache

drop_cache的详细文档如下,以便查阅.

Linux之清理linux内存cache

    Writing to this will cause 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
To free dentries and inodes:
* echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
* echo 3 > /proc/sys/vm/drop_caches
As this is a non-destructive operation, and dirty objects are notfreeable, the user should run "sync" first in order to make sure allcached objects are freed.
This tunable was added in 2.6.16.(只有内核在2.6.16以上的才支持)

Linux之清理linux内存cache

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292