1  Linux下内存占用多的原因

当linux第一次读取一个文件运行时,一份放到一片内存中cache起来,另一份放入运行程序的内存中,正常运行,当程序运行完,关闭了,cache中的那一分却没有释放,第二次运行的时候,系统先看看在内存中是否有一地次运行时存起来的cache中的副本,如果有的话,直接从内存中读取,那样,速度就快多了。

说明这种情况的很典型的例子是启动firefox,由于firefox程序很大,因此第一次读取运行的时候很慢,尤其在速度不快的机器上,但是当你彻底关闭了firefox,ps看不到一个firefox进程,第二次再启动的时候就比第一次明显快很多,这是由于这次系统是直接从cache中读取的firefox来运行,并不是从磁盘上读取的。

再有一个例子:我们频繁使用的ls命令等基本命令,你运行的时候根本看不到硬盘灯闪,因为这些常用的命令都是再第一次运行后就保存在cache中的,以后就一直从内存中读出来运行。

如果cache占用的内存过多了,影响正常运行程序需要的内存,那么会释放掉一部分cache内存,但是总量会保持一个很高的值,所以,linux总是能最大限度的使用内存,就算加到16G,32G内存,也会随着不断的IO操作,内存的free值会慢慢减少到只有几M,想要内存不发生这种情况,只有一个办法:把内存加到比硬盘大。

2 手动释放方法

2.1 使用free查看一下当前内存使用情况(可略过):

[root@*** ~]# free -m
total used free shared buffers cached
Mem: 512 488 23 0 57 157
-/+ buffers/cache: 273 238 Swap: 1055 0 1055

2.2 执行sync同步数据

[root@*** ~]# sync

2.3 清理cache

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

2.4 drop_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.

网上查了查资料,整理一下:

关于drop_caches文件:系统默认为0

在Documentation/sysctl/vm.txt中有如下描述:

drop_caches

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:仅清除页面缓存(PageCache)
 echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:清除目录项和inode
 echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:清除页面缓存,目录项和inode
 echo 3 > /proc/sys/vm/drop_caches

As this is a non-destructive operation and dirty objects are not freeable, the user should run `sync' first.

linux清除cache的方法的更多相关文章

  1. linux中清除cache的方法

    在进行测试文件导入的时候,发现内存占用很大,如下所示: [root@python ~]# vmstat 1 -S M 3 procs -----------memory---------- ---sw ...

  2. Linux 常用命令使用方法大搜刮

    Linux 常用命令使用方法大搜刮 1.# 表示权限用户(如:root),$ 表示普通用户  开机提示:Login:输入用户名  password:输入口令   用户是系统注册用户成功登陆后,可以进入 ...

  3. Linux 释放cache化缓存

    Linux 释放cache化缓存 free -g查看空余内存以及已使用内存 原文  https://blog.csdn.net/tomspcc/article/details/78131468 机械硬 ...

  4. 转:Linux环境变量设置方法总结 PATH、LD_LIBRARY_PATH

    转:  https://www.linuxidc.com/Linux/2017-03/142338.htm 文章写比较全  转载记录 Linux环境变量设置方法总结 PATH.LD_LIBRARY_P ...

  5. Linux Buffer/Cache 的区别

      以前经常使用free -h命令来查看当前操作系统的内存使用情况,同时也注意过返回信息中有一列是buff/cache,来公司之前,面试官还问过我这两个的区别,当时没有回答出来,现在特意回顾记录下: ...

  6. Ubuntu Linux系统三种方法添加本地软件库

    闲着没事教教大家以Ubuntu Linux系统三种方法添加本地软件库,ubuntu Linux使用本地软件包作为安装源——转2007-04-26 19:47新手重新系统的概率很高,每次重装系统后都要经 ...

  7. (转)Linux I/O 调度方法

    Linux I/O 调度方法 转自https://blog.csdn.net/theorytree/article/details/6259104 操作系统的调度有 CPU调度    CPU sche ...

  8. linux dentry cache 转自:http://blog.csdn.net/denzilxu/article/details/9188003

    Linux dentry cache学习 每个dentry对象都属于下列几种状态之一: (1)未使用(unused)状态:该dentry对象的引用计数d_count的值为0,但其d_inode指针仍然 ...

  9. [转载]linux 清除系统cached

    FROM: http://cqfish.blog.51cto.com/622299/197230 linux 清除系统cached top查看系统内存使用情况   Mem:    16432180k ...

随机推荐

  1. LVS三种模式区别

    参考文档 http://www.magedu.com/65436.html 名词:CIP 客户端IP地址   VIP:即DS服务器上的代理IP地址,也是客户端访问的执行IP地址 1.NAT模式 ①.客 ...

  2. JS向固定数组中添加不重复元素并冒泡排序

    向数组{7,20,12,6,25}中添加一个不重复的数字,然后按照从小到大的顺序排列 源代码: <!DOCTYPE html> <html> <head> < ...

  3. Mybatis核心类生命周期和管理

    Mybatis核心类生命周期和管理 原文链接:https://blog.csdn.net/qq1134550437/article/details/51960480 1.SqlSessionFacto ...

  4. 一维消消乐(DP)

    一维消消乐是一款非常简单的游戏.有n颗珠子排成一排,每一颗珠子有一个价值w(可能是负数). 游戏是这样,你可以选择如若干对相邻的珠子,让他们同时消去.每一对珠子的消失,都会使得总分数加上两颗珠子相乘的 ...

  5. 寒假day10

    今天开始写论文,同时爬取并清洗了毕设的人才动态相关部分数据

  6. MySQL--通过.frm和.ibd对mysql数据恢复

    转载:http://bbs.csdn.net/topics/392114182 例如说 现在要恢复user表1.先建立和之前user表一样的表结构.就是执行create table user .... ...

  7. Django2.0——中间件

    Django中间件middleware本质是一个类,在请求到返回的中间,类中不同的方法会在指定的时机中被触发.setting.py的变量MIDDLEWARE_CLASSES中的每一个元素都是中间件,且 ...

  8. Half of UK 10-year-olds own a smartphone

    1. preposition n. 介词  pronoun  n. 代词 2. despite /preposition. (1) used to say that something happens ...

  9. Python—使用列表构造栈数据结构

    class Stack(object): """ 使用列表实现栈 """ def __init__(self): self.stack = ...

  10. JavaSE--【JAVA】unicode为12288字符

    转载:http://blog.csdn.net/zfpigpig/article/details/8186470 今天做一个导入数据功能时发现一个问题,就是一个unicode为12288的字符显示为空 ...