读书人

图1时之快:手动释放Linux服务器内存

发布时间: 2012-07-15 20:11:39 作者: rapoo

图一时之快:手动释放Linux服务器内存

????? 在服务器运行过程中,使用free -m查看服务器内存时,经常会发现free值很小,有些同学就会很紧张,总想采取一些措施,使得free值看起来高一点,心里就比较爽一点。其实,个人觉得这完全是图一时之快,没什么实质性用途。

一、大众释放内存方法

1. 首先使用free -m查看剩余内存view plaincopy to clipboardprint?
  1. linux-8v2i:~?#?free?-m??
  2. ?????????????total???????used???????free?????shared????buffers?????cached??
  3. Mem:??????????3952???????2773???????178??????????0?????????130????????1097??
  4. -/+?buffers/cache:???????1545???????2406??
  5. Swap:?????????2055??????????0???????2055??
?

2. 执行sync命令?? ? ?使用sync命令以确保文件系统的完整性,sync 命令运行 sync 子例程,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件。view plaincopy to clipboardprint?
  1. linux-8v2i:~?#?sync??
?

3. 修改/proc/sys/vm/drop_cachesview plaincopy to clipboardprint?
  1. echo?3?>?/proc/sys/vm/drop_caches??
?说明:1>. /proc是一个虚拟文件系统,我们可以通过对它的读写操作作为与kernel实体间进行通信的一种手段。也就是说可以通过修改/proc中的文件,来对当前kernel的行为做出调整。也就是说我们可以通过调整/proc/sys/vm/drop_caches来释放内存。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。

4.再使用free -m查看剩余内存,情况如下:view plaincopy to clipboardprint?
  1. linux-8v2i:~?#?free?-m??
  2. ?????????????total???????used???????free?????shared????buffers?????cached??
  3. Mem:??????????3952???????1773???????2325??????????0?????????0????????80??
  4. -/+?buffers/cache:???????1545???????2406??
  5. Swap:?????????2055??????????0???????2055??
?

二、Linux内存分析

1. 首先对free -m查看结果进行分析view plaincopy to clipboardprint?
  1. linux-8v2i:~?#?free?-m??
  2. ?????????????total???????used???????free?????shared????buffers?????cached??
  3. Mem:??????????3952???????2773???????178??????????0?????????130????????1097??
  4. -/+?buffers/cache:???????1545???????2406??
  5. Swap:?????????2055??????????0???????2055??
?各参数含义:

读书人网 >UNIXLINUX

热点推荐