您的位置:首页 > 运维架构 > Linux

linux 内存相关问题

2013-04-01 10:46 232 查看
linux 内存相关问题

今天收到服务器的内存报警信息。比如下面的例子

free -m

total used free shared buffers cached

Mem: 1875 1589 286 0 127 542

-/+ buffers/cache: 920 955

Swap: 4031 70 396



其中真正使用的内存数量与used的值是不成正比的。

真正剩余的内存数量是free+cached

即便是一个刚装上不久的linux系统 ,如果有频繁的文件访问,就会导致cache使用量增大,从而内存使用量这个值会很高,乃至90%也很正常。但是,这个内存占用量不会达到100%的,因为当达到一个占用量的时候,就会启用swap(虚拟内存)。

而使用内存高的原因是:

1.linux把剩余的内存都用做磁盘缓存了,这样可以减少硬盘读写次数,Linux在磁盘操作上的速度是要比windows快的。这样做并不会影响系统速度。换句话说就是Linux 是先占用,然后再慢慢使用,windows是要用多少就占用多少。Linux 不会让内存空着的,程序用不了就当缓存用。

2.桌面追求的是用户处理的反应速度,而服务器则是承载量,linux正式服务器系统中的佼佼者,着重体现在任务的调度和内存的管理等。而从桌面的角度讲,一定要保留足够的空闲内存去快速反应。

这个占用内存高固然不会影响使用,但是需要注意去使用top等相关的命令,查看程序真正占用的内存数是不是很高。如果确定没问题,那就是真的没问题了。

关于使用的内存是可以手动进行清理的。

运行sync将dirty的内容写回硬盘

$sync

通过修改proc系统的drop_caches清理free的cache

$echo 3 > /proc/sys/vm/drop_caches

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.

修改/etc/sysctl.conf 添加如下选项后就不会内存持续增加(不推荐使用)

vm.dirty_ratio = 1

vm.dirty_background_ratio=1

vm.dirty_writeback_centisecs=2

vm.dirty_expire_centisecs=3

vm.drop_caches=3

vm.swappiness =100

vm.vfs_cache_pressure=163

vm.overcommit_memory=2

vm.lowmem_reserve_ratio=32 32 8

kern.maxvnodes=3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: