我对linux内存缓存的这种行为感到很困惑.
total used free shared buffers cached
Mem: 15953 14188 1765 64 37 11504
-/+ buffers/cache: 2645 13308
Swap: 2047 1332 715
不应该具有针对缓存的非缓存内存优先级?换句话说:为什么机器交换到磁盘而不是丢弃缓存.
我可以改变这种行为吗?如果有,怎么样?
解决方法:
linux交换算法使用“最近使用过的页面”的概念.虚拟内存中的每个页面都有一个与之关联的年龄.如果频繁访问该页面,那么该页面应该是相当年轻的,而如果没有访问页面,则该页面变得更旧.页面越老,它们就越有可能被换出.
因此,如果内核交换了内容,那么这是因为这些页面的年龄(与其他页面相比)是旧的.如果所有页面都有足够的物理内存,无论其年龄如何,都不会交换任何内容.
内核被配置为以最有效的方式处理他的资源,例如内存和交换,这是可能的.
我认为你不应该改变这种行为.但是,如果您愿意,可以更改系统swappiness. swappiness设置为0表示除非绝对必要(内存不足),否则将避免使用磁盘.
从Kernel Documentation关于swappiness的价值:
This control is used to define how aggressive the kernel will swap
memory pages. Higher values will increase agressiveness, lower values
decrease the amount of swap. A value of 0 instructs the kernel not to
initiate swap until the amount of free and file-backed pages is less
than the high water mark in a zone.
在linux内核源代码中,文件vmscan.c处理swappiness值.这是有趣的部分:
2018 /*
2019 * With swappiness at 100, anonymous and file have the same priority.
2020 * This scanning priority is essentially the inverse of IO cost.
2021 */
2022 anon_prio = swappiness;
2023 file_prio = 200 - anon_prio;
>匿名页面是内存映射,没有文件或设备支持它.这是程序从操作系统分配内存以供堆栈和堆等使用的方式.
>文件页面镜像现有文件的内容.
正如您在上面的源代码片段中看到的,交换文件页面的优先级(默认值为60)高于交换匿名页面的优先级.但是,如果设置为100,则两个值具有相同的优先级.如果设置为0,则优先级差异尽可能大.
您可以按如下方式设置swappiness:
echo n >/proc/sys/vm/swappiness
…其中n是0-100的值.
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!