免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5990 | 回复: 12
打印 上一主题 下一主题

内存中的buffers和cache有什么区别 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-09 10:39 |只看该作者 |倒序浏览
free命令中的buffers和cached是什么意思呢?shared又是什么意思,高手指点一下

             total       used       free     shared    buffers     cached
Mem:       1028396     392104     636292          0      12496     158420
-/+ buffers/cache:     221188     807208
Swap:       385520         24     385496

论坛徽章:
0
2 [报告]
发表于 2006-01-09 10:44 |只看该作者
同问。
我发现在大量printk时,cache的使用迅速上升。我用的vmstat来显示。

论坛徽章:
0
3 [报告]
发表于 2006-01-09 10:57 |只看该作者
buffer:缓存着文件结点之类的信息
cache:缓存着读写过的文件内容信息

Overview of memory management
Traditional Unix tools like 'top' often report a surprisingly small amount of free memory after a system has been running for a while. For instance, after about 3 hours of uptime, the machine I'm writing this on reports under 60 MB of free memory, even though I have 512 MB of RAM on the system. Where does it all go?

The biggest place it's being used is in the disk cache, which is currently over 290 MB. This is reported by top as "cached". Cached memory is essentially free, in that it can be replaced quickly if a running (or newly starting) program needs the memory.

The reason Linux uses so much memory for disk cache is because the RAM is wasted if it isn't used. Keeping the cache means that if something needs the same data again, there's a good chance it will still be in the cache in memory. Fetching the information from there is around 1,000 times quicker than getting it from the hard disk. If it's not found in the cache, the hard disk needs to be read anyway, but in that case nothing has been lost in time.

To see a better estimation of how much memory is really free for applications to use, run the command free -m:

Code: free -m  
             total       used       free     shared    buffers     cached
Mem:           503        451         52          0         14        293
-/+ buffers/cache:        143        360
Swap:         1027          0       1027


The -/+ buffers/cache line shows how much memory is used and free from the perspective of the applications. Generally speaking, if little swap is being used, memory usage isn't impacting performance at all.

Notice that I have 512 MB of memory in my machine, but only 503 is listed as available by free. This is mainly because the kernel can't be swapped out, so the memory it occupies could never be freed. There may also be regions of memory reserved for/by the hardware for other purposes as well, depending on the system architecture.

论坛徽章:
0
4 [报告]
发表于 2006-01-09 11:16 |只看该作者
shard是表示内存页的使用者超过1吗?而且是只读,如果有写操作,就写时拷贝(cow)吗?

论坛徽章:
0
5 [报告]
发表于 2006-01-09 12:52 |只看该作者
shared列显示多个进程共享的内存总额;但是并不是你所说的内存页的使用者超过1,如果这样,共享库都属于这类了,但是它一般情况下却为0,所以个人认为,这项应该是应用程序中明确指出某段内存需要和另一些程序共享,以此来进行进程间通信

论坛徽章:
0
6 [报告]
发表于 2006-01-09 13:12 |只看该作者
这个和进程通信里的 共享内存 不是一回事情吧.

论坛徽章:
0
7 [报告]
发表于 2006-01-09 13:58 |只看该作者
是这样的,在Linux中,管道、IPC共享线性区都是作为特殊文件系统来对待的,这样才能象操作文件一样操作管道和共享线性区(当然sockfs也是),这样,它们就可能会单独拿出来统计。

论坛徽章:
0
8 [报告]
发表于 2006-01-09 14:26 |只看该作者
那么用free看到的shared和用top看到的每进程的SHR是一回事吗?
因为我用free,看到shared是0,但是用top看到的却是很多进程的SHR都不是0

论坛徽章:
0
9 [报告]
发表于 2006-01-10 13:35 |只看该作者
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
0  0      0 492364   1580  10876    0    0     2     1 2589     4  0 64 36  0
0  0      0 492364   1588  10876    0    0     0    12 10568     3  0 22 78  0
0  0      0 492364   1596  10876    0    0     0     3 12724     3  0 25 75  0
0  0      0 492364   1604  10876    0    0     0     3 16038     3  0 28 72  0
0  0      0 492364   1612  10876    0    0     0     3 16005     3  0 29 71  0
0  0      0 492364   1620  10876    0    0     0     3 12571     2  0 23 77  0
0  0      0 492364   1628  10876    0    0     0     3 11275     3  0 23 77  0
0  0      0 492364   1636  10876    0    0     0     3 11287     3  0 21 79  0
0  0      0 492364   1644  10876    0    0     0     6 11301     3  0 22 78  0
0  0      0 492364   1652  10876    0    0     0     9 11357     3  0 24 76  0
0  0      0 492364   1660  10876    0    0     0     3 11267     3  0 23 77  0
0  0      0 492364   1668  10876    0    0     0     3 11580     2  0 21 79  0
0  0      0 492364   1676  10876    0    0     0     3 11363     3  0 23 77  0
0  0      0 492364   1684  10876    0    0     0     3 11512     2  0 22 78  0
0  0      0 492364   1692  10876    0    0     0     3 11346     3  0 23 77  0
0  0      0 492364   1700  10876    0    0     0     3 11405     3  0 20 80  0


这是我系统上的内存使用情况,vmstat 5显示的,有个问题,为什么buffer的数量一直在增加而free的内存数量不变呢?

论坛徽章:
0
10 [报告]
发表于 2006-01-10 13:48 |只看该作者
这是可能的啊,因为你对文件的操作(执行文件也要读文件),然后释放了读来的数据,然后释放后的内存就进入buffer了,而不是直接归还给系统,系统在内存不够用时,会自动把buffer内存释放,而且内核会对buffer的内存页进行动态的状态调整,以便于逐步释放。

出现这种情况有很多可能,也许你的系统一直没有再运行,在不停的释放页面,但是都不需要malloc操作啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP