原文 http://blog.zol.com.cn/2322/article_2321774.html

#cat /proc/meminfo | grep "MemFree" | awk '{print $2}'    (用来查看free的内存有多少)之前先了解一下内存的基本知识#free -mtotal       used       free     shared    buffers     cached
Mem:         1002        769        232          0         62        421
-/+ buffers/cache:        286        715
Swap:         1153          0       1153

第一部分Mem行:
total 内存总数: 1002M
used 已经使用的内存数: 769M
free 空闲的内存数: 232M
shared 当前已经废弃不用,总是0
buffers Buffer 缓存内存数: 62M
cached Page 缓存内存数:421M

关系:total(1002M) = used(769M) + free(232M)

第二部分(-/+ buffers/cache):
(-buffers/cache) used内存数:286M (指的第一部分Mem行中的used - buffers - cached)
(+buffers/cache) free内存数: 715M (指的第一部分Mem行中的free + buffers + cached)

可见-buffers/cache反映的是被程序实实在在吃掉的内存,而+buffers/cache反映的是可以挪用的内存总数。(从这里我们可
以看出,实际上 :可用内存=第一部分Mem行中的free + buffers + cached,并不是只有free部分)
#sync
#sync
#echo 3   > /proc/sys/vm/drop_caches
#free -m

[root@server test]# cat /proc/sys/vm/drop_caches
0
首先,/proc/sys/vm/drop_caches的值,默认为0

[root@server test]# sync    #把内存里的数据暂时写到硬盘里

手动执行sync命令(描述:sync 命令运行 sync 子例程。如果必须停止系统,则运行 sync 命令以确保文件系统的完整性。sync 命令将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件)

[root@server test]# echo 3 > /proc/sys/vm/drop_caches
[root@server test]# cat /proc/sys/vm/drop_caches
3

将/proc/sys/vm/drop_caches值设为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

再来运行free命令,发现现在的used为66MB,free为182MB,buffers为0MB,cached为11MB.那么有效的释放了buffer和cache.

有关/proc/sys/vm/drop_caches的用法在下面进行了说明

/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, 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(8) first.

--内存释放前

#sync

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

内存释放后:

echo 2 > /proc/sys/vm/drop_caches

echo 3 > /proc/sys/vm/drop_caches

cache释放:

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

说明,释放前最好sync一下,防止丢数据。

因为LINUX的内核机制,一般情况下不需要特意去释放已经使用的cache。这些cache起来的内容可以增加文件以及的读写速度。

查看内存条数命令:

#dmidecode |grep -A16 "Memory Device$"

先说下free命令怎么看内存

[root@yuyii proc]# free

total used free shared buffers cached

Mem: 515588 295452 220136 0 2060 64040

-/+ buffers/cache: 229352 286236

Swap: 682720 112 682608

其中第一行用全局角度描述系统使用的内存状况:

total——总物理内存

used——已使用内存,一般情况这个值会比较大,因为这个值包括了cache+应用程序使用的内存

free——完全未被使用的内存

shared——应用程序共享内存

buffers——缓存,主要用于目录方面,inode值等(ls大目录可看到这个值增加)

cached——缓存,用于已打开的文件

note:

total=used+free

used=buffers+cached (maybe add shared also)

第二行描述应用程序的内存使用:

前个值表示-buffers/cache——应用程序使用的内存大小,used减去缓存值

后个值表示+buffers/cache——所有可供应用程序使用的内存大小,free加上缓存值

note:

-buffers/cache=used-buffers-cached

+buffers/cache=free+buffers+cached

第三行表示swap的使用:

used——已使用

free——未使用

cache释放:

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

说明,释放前最好sync一下,防止丢数据。

附:

linux cache内存释放bash 脚本

#! /bin/sh
    freemem=$(cat /proc/meminfo | grep "MemFree" | awk '{print $2}')
       if [ $freemem -le 409600 ]
          then
               date  >> /var/log/mem.log
               free -m >> /var/log/mem.log
               sync
               sync
               echo 3 > /proc/sys/vm/drop_caches
               free -m >> /var/log/mem.log            
       fi

转 linux 内存释放的更多相关文章

  1. 谈谈Linux内存释放

    上上周吧,一个朋友问我说他公司的服务器内存free 为0 是为什么,意思大概是内存去哪了,这引发了一个小小的讨论,也就是内存释放的问题… 首先我们可能会用free 去查看内存的使用率,它应该是这样的 ...

  2. Linux 内存释放命令非常不错具有参考借鉴价值

    这篇文章主要介绍了Linux 内存释放命令的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下(http://www.120xcjl.com) 我使用的是CentOS 6.5 ,由于卸载Solr ...

  3. Linux 内存释放

    简介 linux 内存释放通过如下命令,将cache与buff根据环境进行释放操作,避免重启释放内存. 操作 1.将内存中buff数据保存磁盘 sync 2.清理cache与buff缓存 echo 3 ...

  4. linux 内存释放命令

    我使用的是CentOS 6.5 ,由于卸载Solr 后发现内存占用挺多的,我想释放一下内存,就查阅了一些资料,分享给大家: 1.free -m  查看内存的使用情况,-m表示单位是兆 2.echo 1 ...

  5. Linux Kernel 释放后重用内存损坏漏洞

    漏洞名称: Linux Kernel 释放后重用内存损坏漏洞 CNNVD编号: CNNVD-201307-305 发布时间: 2013-07-18 更新时间: 2013-07-18 危害等级:    ...

  6. 手工释放Linux内存

    转载自:http://blog.csdn.net/wyzxg/article/details/7279986/ linux的内存查看: [root@localhost 0.1.0]# free -m  ...

  7. 手工释放linux内存——/proc/sys/vm/drop_caches

    --手工释放linux内存——/proc/sys/vm/drop_caches 总有很多朋友对于Linux的内存管理有疑问,之前一篇日志似乎也没能清除大家的疑虑.而在新版核心中,似乎对这个问题提供了新 ...

  8. Linux内存机制以及手动释放swap和内存

    今天我们来谈谈Linux的内存机制. 首先我们理一下概念 一.什么是linux的内存机制? 我们知道,直接从物理内存读写数据要比从硬盘读写数据要快的多,因此,我们希望所有数据的读取和写入都在内存完成, ...

  9. (转)手工释放linux内存——/proc/sys/vm/drop_cache

    linux的内存查看: [root@localhost 0.1.0]# free -m                   total       used       free     shared ...

随机推荐

  1. spring源码研究之IoC容器在web容器中初始化过程

    转载自 http://ljbal.iteye.com/blog/497314 前段时间在公司做了一个项目,项目用了spring框架实现,WEB容器是Tomct 5,虽然说把项目做完了,但是一直对spr ...

  2. [转载]Java线程的两种实现方式

    转载:http://baijiahao.baidu.com/s?id=1602265641578157555&wfr=spider&for=pc 前言 线程是程序的一条执行线索,执行路 ...

  3. [Erlang09]Erlang gen_server实现定时器(interval)的几种方法及各自的优缺点?

    方法1: %%gen_server:部分call_back function. -define(TIME,1000). init([]) –> erlang:send_after(?TIME,s ...

  4. Backup--备份相关的信息查看及小技巧

    --查看指定数据库当前最小 LSN DECLARE @database_name NVARCHAR( 200) SET @database_name ='DBName' SELECT  MIN (re ...

  5. border使用小技巧

    border-style 分类 dashed虚线类型 dotted 点线类型 double 双线类型 双线型量根实线的宽度和中间空白区域的间距有一定规律: 可以利用这个规律画出一些特殊的图案 代码如下 ...

  6. Redis 工具类

    项目里的Redis 工具类,写下来以备后用 public class RedisConnector { public class RedisParseResult<T> { public ...

  7. 使用SQL Delta.v5.1.1.98.破解版同步数据结构

    概述 本篇文章主要介绍SQL DELTA的简单使用.为了能够更加明了的说明其功能,本文将通过实际项目中的案例加以介绍. 1. SQLDELTA简介 SQLDELTA是一款便捷实用的数据库管理工具.使用 ...

  8. Perl+OpenGL 重绘inkscape生成的svg矢量图

    Perl+OpenGL 重绘inkscape生成的svg矢量图 还不够完善,先挖个坑,后面慢慢填 Code: [全选] [展开/收缩] [Download] (Untitled.pl) =info A ...

  9. Linux多网口绑定配合华为5700 eth-trunk技术,提高网络性能

    在实际的环境中,服务器通过网口绑定技术,可以很容易的实现网口冗余,负载均衡,从而达到高可用的目的,而且可以提升网络的性能,大幅的提升网络I/O. 一般情况下,Linux的多网口绑定使用的是内核中的“b ...

  10. [ActionScript 3.0] 自制简单拾色器

    colorBoard为库中绑定的影片剪辑,colorBoard中包含影片剪辑currColor,文本colorText,影片剪辑close: colorDot为库中绑定的影片剪辑,colorDot中包 ...