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. Lock wait timeout exceeded; try restarting transaction(mysql事务锁)

    现场环境客户要求删数据(界面没法直接操作),于是直接在数据库进行查询删除了,删完发现界面依然能查到删除后的数据,又用sql语句进行删除,发现报了错:Lock wait timeout exceeded ...

  2. h5-transform-3d

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Q1:Two Sum

    1. Two Sum 官方的链接:1. Two Sum Description : Given an array of integers, return indices of the two numb ...

  4. JavaScipt 动画引擎

    队列操作 jquery中有一个Queue队列的接口,这个模块没有单独拿出来作为一个章节是因为这个是内部专门为动画服务的,Queue队列如同data数据缓存与Deferred异步模型一样,都是jQuer ...

  5. 数字证书原理(ssl,https)

    https://blog.csdn.net/qq_34115899/article/details/81298284 关于私钥公钥数字签名数字证书.https.RSA的一些讲解 http://www. ...

  6. nginx 负载策略

    1.轮询(weight=1) 默认选项,当weight不指定时,各服务器weight相同,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. upstream bak ...

  7. Jetson TX2入门学习之Ubuntu默认密码

    在使用TX2开发板时进行软件更新时需要身份验证,TX2默认有两个登录身份,一个是ubuntu 一个是nvidia 登录其中的哪一个都可以更新   两个身份的密码和登录名是一样的用户:ubuntu 密码 ...

  8. MyBatis项目报错:The server time zone value '�й�׼' is unrecognized or represents more than one time zone

    问题来源:在部署Spring + MyBatis项目的时候,运行时报错The server time zone value '�й�׼' is unrecognized or represents m ...

  9. String,StringBuffer与StringBuilder的区别与选择

    三者的区别 String:不可变类,一旦一个对象被建立的时候,包含在这个对象中的字符串序列是不可变的,直到这个对象被销毁.StringBuffer:可变字符序列的字符串.当其对象被创建的时候,可以用a ...

  10. Bug(1)

    程序要求:内网之间客户端截屏(.bmp)并传送给服务端. server: #include <winsock2.h> // 为了使用Winsock API函数 #include <s ...