Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6895512
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类: 架构设计与优化

2015-08-12 15:57:04

Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决

[日期:2015-08-12] 来源:Linux社区  作者:panfeng412 [字体:  ]

问题描述

Linux服务器内存使用量超过阈值,触发报警。

问题排查

首先,通过free命令观察系统的内存使用情况,显示如下:

             total       used free shared    buffers     cached
Mem: 24675796 24587144 88652 0 357012 1612488 -/+ buffers/cache: 22617644 2058152 Swap: 2096472 108224 1988248

其中,可以看出内存总量为24675796KB,已使用22617644KB,只剩余2058152KB。

然后,接着通过top命令,shift + M按内存排序后,观察系统中使用内存最大的进程情况,发现只占用了18GB内存,其他进程均很小,可忽略。

因此,还有将近4GB内存(22617644KB-18GB,约4GB)用到什么地方了呢?

进一步,通过cat /proc/meminfo发现,其中有将近4GB(3688732 KB)的Slab内存:

......
Mapped: 25212 kB
Slab: 3688732 kB
PageTables: 43524 kB
......

Slab是用于存放内核数据结构缓存,再通过slabtop命令查看这部分内存的使用情况:

  OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME 13926348 13926348 100% 0.21K 773686 18 3494744K dentry_cache 334040 262056 78% 0.09K 8351 40 33404K buffer_head 151040 150537 99% 0.74K 30208 5 120832K ext3_inode_cache

发现其中大部分(大约3.5GB)都是用于了dentry_cache

问题解决

1. 修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档):

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 notfreeable, the user should run "sync" first in order to make sure allcached objects are freed.
This tunable was added in 2.6.16.

2. 方法1需要用户具有root权限,如果不是root,但有sudo权限,可以通过sysctl命令进行设置:

$sync $sudo sysctl -w vm.drop_caches=3 $sudo sysctl -w vm.drop_caches=0 #recovery drop_caches

操作后可以通过sudo sysctl -a | grep drop_caches查看是否生效。

3. 修改/proc/sys/vm/vfs_cache_pressure,调整清理inode/dentry caches的优先级(默认为100),LinuxInsight中有相关的解释:

At the default value of vfs_cache_pressure = 100 the kernel will attempt to reclaim dentries and inodes at a “fair” rate with respect to pagecache and swapcache reclaim. Decreasing vfs_cache_pressure causes the kernel to prefer to retain dentry and inode caches. Increasing vfs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes.

具体的设置方法,可以参考方法1或者方法2均可。

参考资料

https://www.kernel.org/doc/Documentation/sysctl/vm.txt

更多详情见请继续阅读下一页的精彩内容: 

阅读(777) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~