简单介绍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服务器内存释放工作也就轻而易举的完成了。

  1. 详细介绍Linux shell脚本基础学习一)
  2. 简单介绍Linux进程文件路径的获取方法
  3. 详细介绍Linux shell脚本基础学习八)
  4. Linux系统安全提高之安全配置
  5. Linux系统安全提高之系统优化

相关内容

    暂无相关文章