返回首页

简单介绍Linux内存手动释放方法

时间:2013-03-15 13:53来源:转载 作者:未知 点击:
Linux服务器内存释放在有些人看起来好像可以提高不少的效率,但是它实际的工作效果并不是很理想,这里我们说一下如何进行这份工作,帮助我们更好的去理解Linux系统的工作方式。
  

Linux服务器内存释放在有些人看起来好像可以提高不少的效率,但是它实际的工作效果并不是很理想,这里我们说一下如何进行这份工作,帮助我们更好的去理解Linux系统的工作方式。

大众释放内存方法

1. 首先使用free -m查看剩余内存

view plaincopy to clipboardprint?

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 2773 178 0 130 1097

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 2773 178 0 130 1097

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

2. 执行sync命令

使用sync命令以确保文件系统的完整性,sync 命令运行 sync 子例程,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件。

view plaincopy to clipboardprint?

linux-8v2i:~ # sync

linux-8v2i:~ # sync

3. 修改/proc/sys/vm/drop_caches

view plaincopy to clipboardprint?

echo 3 > /proc/sys/vm/drop_caches

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?

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 1773 2325 0 0 80

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 1773 2325 0 0 80

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

Linux服务器内存释放工作也就轻而易举的完成了。

 

相关课程链接嵌入式linux就业班

------分隔线----------------------------

  • 李老师
  • 李老师
  • 胡老师
  • 胡老师
合作伙伴
  • 武汉工程大学合作培训机构

  • 国家信息技术紧缺人才培养工程(NITE)

  • ARM公司全球授权培训中心

  • 国内首家Symbian授权培训

  • 微软全球嵌入式合作伙伴

  • Altera全球合作培训机构

在线客服
  • 客服热线:
    139-8620-3604
    159-2752-9536



  • 官方微信
  •  咨询微信二维码