linux手动释放内存

[root@rusky ~]# free -m

                   total       used       free     shared    buffers     cached

Mem:          1011        343        667          0         34        218

-/+ buffers/cache:         91        919

Swap:         1992          0       1992

[root@rusky ~]# sync    把内存dirty数据同步到硬盘

[root@rusky ~]# echo 3 > /proc/sys/vm/drop_caches

[root@rusky ~]# echo 0 > /proc/sys/vm/drop_caches

[root@rusky ~]# free -m

                   total       used       free     shared    buffers     cached

Mem:          1011        124        886          0          0         36

-/+ buffers/cache:         87        923

Swap:         1992          0       1992

  注解:/proc是一个虚拟文件系统,我们可以通过对它的读写操作做为与kernel实体间进行通信的一种手段。也就是说可以通过修改 /proc中的文件,来对当前kernel的行为做出调整。那么我们就可以通过调整/proc/sys/vm/drop_caches来释放内存。

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

==============================================================

drop_caches

Writing to this will cause the kernel to drop clean caches, as well as reclaimable slab objects like dentries and inodes.  Once dropped, their memory becomes free.

To free pagecache:

         echo 1 > /proc/sys/vm/drop_caches

To free reclaimable slab objects (includes dentries and inodes):

         echo 2 > /proc/sys/vm/drop_caches

To free slab objects and pagecache:

         echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will not free any dirty objects. To increase the number of objects freed by this operation, the user may run `sync' prior to writing to /proc/sys/vm/drop_caches.  This will minimize the number of dirty objects on the system and create more candidates to be dropped.

This file is not a means to control the growth of the various kernel caches (inodes, dentries, pagecache, etc...)  These objects are automatically reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems.  Since it discards cached objects, it may cost a significant amount of I/O and CPU to recreate the dropped objects, especially if they were under heavy use.  Because of this, use outside of a testing or debugging environment is not recommended.

You may see informational messages in your kernel log when this file is used: cat (1234): drop_caches: 3

These are informational only.  They do not mean that anything is wrong with your system.  To disable them, echo 4 (bit 3) into drop_caches.

==============================================================

posted on 2014-06-03 14:50  遠離塵世の方舟  阅读(308)  评论(0编辑  收藏  举报

导航