linux 进程 释放内存,Linux 释放内存方法和原理

今天惊愕地发现,主节点上8G内存被不知道什么进程吃掉了整整6G有余,正常的计算快要维持不下去了,遂处理之。

先看看内存使用状况

[root@node1 ~]# free -m

total used free shared buffers cached

Mem: 8004 6557 1446 0 163 5630

-/+ buffers/cache: 763 7240

Swap: 1983 0 1983

其中:

total 内存总数

used 已经使用的内存数

free 空闲的内存数

shared 多个进程共享的内存总额

buffers Buffer Cache和cached Page Cache 磁盘缓存的大小

-buffers/cache 的内存数:used - buffers - cached

+buffers/cache 的内存数:free + buffers + cached

可用的memory=free memory+buffers+cached

把内存里的数据暂时写到硬盘里

[root@node1 ~]# sync

修改 /proc/sys/vm/drop_caches文件(/proc/sys/vm/drop_caches的默认值为0)

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

[root@node1 ~]# cat /proc/sys/vm/drop_caches

再看内存

[root@node1 ~]# free -m

total used free shared buffers cached

Mem: 8004 631 7372 0 0 60

-/+ buffers/cache: 570 7433

Swap: 1983 0 1983

终于释放出来了。

有关/proc/sys/vm/drop_caches的用法在下面进行了说明(原文:http://www.linuxinsight.com/proc_sys_vm_drop_caches.html):

Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.

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

As this is a non-destructive operation, and dirty objects are not freeable, the user should run "sync" first in order to make sure all cached objects are freed.

This tunable was added in 2.6.16.

真实情况是 “当有某程序需要占用大内存时linux会自动释放cache”

linux的理念是内存空着不用白不用,因此会使用空闲内存来做buffer和cache。

我的一台虚拟机运行了JBOSS,在运行一段时间后,查看内存使用情况:

[root@JBOSS ~]# free -m

total used free shared buffers cached

Mem: 3949 2928 1021 0 308 1278

-/+ buffers/cache: 1341 2608

Swap: 4094 0 4094

可以看出,内存已用2928M,但实际除去 buffers/cache后实际只使用了1341M。

你若不管它,内存是不会释放的,而且也没必要释放。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值