Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208745
  • 博文数量: 53
  • 博客积分: 2059
  • 博客等级: 大尉
  • 技术积分: 577
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-30 03:03
文章分类
文章存档

2012年(4)

2011年(19)

2010年(30)

分类: LINUX

2010-12-07 20:07:30

 
  最近在生产环境上,有几台服务器4G内存,发现内存大部分都占用在3.7G左右,这样子看起来很吓人,通过top,ps 来查看也没有发现有什么程序占用大量的内存,于是在网上找了一下关于Linux内存的资料,差不多能够理解Linux内存的机制。
在linux下查看内存,一般是用free -m 命令
[root@urg-nagios ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1003        944         58          0        274        448
-/+ buffers/cache:        221        781
Swap:         2015          0       2015
在CU,上有一个帖子是专门来讨论Linux内存机制的,这里先说明下上面的相关说明吧
第一行,
totle:总共内存大小
used:已使用大小
free:可用大小
shared:多个进程共享内存的大小
buffers/cached:暂时理解为缓存大小
第二行:mem 即内存 对应上面的totle used free等的大小
第三行:-/+ buffers/cache:
used:已使用多大
free:可用有多少
第四行:显示的swap可用大小
第二行(mem)的used/free与第三行(-/+ buffers/cache) used/free的区别。
这两个的区别在于使用角度来看,第一行是从OS的角度来看,因为对于OS,buffers/cached(暂时理解为缓存)都是属于被使用,所以他的可用内存是58M,应用内存是:944M,其中包括内核(OS)的使用+ Application (如Mysql,httpd等程序使用的内存)+buffers+cached
第三行所指的是从应用程序角度来看的,对于应用程序来说,buffers/cached 是等于可用的,因为buffer/cached是为了提供文件的读取的性能,当应用程序在用到内存的时候,buffer/cached会很快地被回收。所以从应用程序的角度来说,可用内存=系统free memory+buffers+cached
简单的说,就是在Linux系统中,当系统需要访问一些文件的时候,会把这些常用的文件缓存在内存中,以至于内存占用很大,但是这些内存是可以快速回收的。即使内存占用很大的时候,只要交换分区没有大量使用,是不用担心的。
**********
下面解释一下 cache和buffers
在之前说过一个CU的帖子,对内存的讨论,地址是:
由于本人是菜鸟,对上面的大牛的讨论,只能理解其中的一部分,所以说一下个人对cache和buffers的理解
,有错误之处,请发邮件指出。
A buffer is something that has yet to be "written" to disk. A cache is something that has been "read" from the disk and stored for later use.

More information provided from moderator
Buffers are allocated by various processes to use as input queues, etc. A simplistic explanation of buffers is that they allow processes to temporarily store input in memory until the process can deal with it.

Cache is typically frequently requested disk I/O. If multiple processes are accessing the same files, much of those files will be cached to improve performance (RAM being so much faster than hard drives).
***********************************************
在讨论中,有以上内容,可以简单的理解
buffer(缓冲):  缓冲是为了程序以后读取数据更快速而分配的,大部份时候应该是程序输出的内容,就是C里面程序的输出到文件的 那个文件的buffer (这个应该是文件相关的)
cache(缓存): 指的是那些频繁求的IO,如果多个进程访问同一文件,它就会被缓存,以提高性能  (这个应该指是磁盘缓存)
**********上面的理解也一定完全正确,简单的说buffer和cache 不能只是程序写和读的缓存那么简单,但是对于理解上,我们可以这样简单的理解。
对于该讨论还有RedHat相关解释
地址:

Example of "cat /proc/meminfo":

root:	total:    	used:    	free:  		shared:	buffers:	cached:
Mem:  	1055760384	1041887232	13873152	0	100417536 	711233536
Swap: 	1077501952  	8540160 	1068961792
						
MemTotal:		1031016 kB	
MemFree:		13548 kB
MemShared:		0 kB
Buffers:		98064 kB
Cached:			692320 kB
SwapCached:		2244 kB
Active:			563112 kB
Inact_dirty:		309584 kB
Inact_clean:		79508 kB
Inact_target:		190440 kB
HighTotal:		130992 kB
HighFree:		1876 kB
LowTotal:		900024 kB
LowFree:		11672 kB
SwapTotal:		1052248 kB
SwapFree:		1043908 kB
Committed_AS:		332340 kB
						

The information comes in the form of both high-level and low-level statistics. At the top you see a quick summary of the most common values people would like to look at. Below you find the individual values we will discuss. First we will discuss the high-level statistics.

High-Level Statistics

  • MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code)
  • MemFree: Is sum of LowFree+HighFree (overall stat)
  • MemShared: 0; is here for compat reasons but always zero.
  • Buffers: Memory in buffer cache. mostly useless as metric nowadays
  • Cached: Memory in the pagecache (diskcache) minus SwapCache
  • SwapCache: Memory that once was swapped out, is swapped back in but still also is in the swapfile (if memory is needed it doesn't need to be swapped out AGAIN because it is already in the swapfile. This saves I/O)

*************************************

说了以上这么多,我们在回到本文的开头,

由于本人的生产环境,有很多服务器大量的占用内存,所以一直没有找到很好的办法去解决,其实也不需要解决的,因为那些占用内存的是cache和buffers占用了。

[root@*** ~]# free -m

             total       used       free     shared    buffers     cached

Mem:          3949       3704        244          0        537       2406

-/+ buffers/cache:        760       3189

Swap:         8189          0       8189

 

由于,该生产服务器在每天备份一个数据库,在备份的时候,会把备份的数据库存放在本地,所以,就想到是不是数据库备份产生的大文件,占用了大量的内存即缓存在内存中,才使得内存大量被占用,于是删除了本地备份7天前的备份数据,这样子重新查看发现内存使用变小了

通过对比发现是cached变小的结果,那么可以证明是大文件缓存在内存中,导致内存占用很高。

\[root@ ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3949       2358       1590          0        464       1092
-/+ buffers/cache:        801       3148
Swap:         8189          0       8189

 

 

 

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

chinaunix网友2010-12-08 15:10:52

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com