Linux性能调优--虚拟内存

简单地说,虚拟内存就是硬盘中的一块区域,它用来存放内存里使用频率不高的页面文件。好处是为了提高内存的利用率 - 让使用频率高的页面文件活动在内存区域中,提高CPU对数据操作的速度。在Linux中,这个区域叫做swap,一般大小应设置为物理内存的2倍。

    vm.swappiness是Linux内核的一个参数,范围是0~100。它表示实际内存和虚拟内存区域进行数据交换的倾向性大小,数值越大表示倾向性越大,即交换的页面文件越多,反之亦然。一般默认值为60。可用'cat /proc/sys/vm/swappiness’查看。

    这个值应该设置成多大才能提高Linux的性能呢?这个当然要由具体的环境来定了。我在一台CentOS机器上,分别把值设为0,60,100,下面是运行'vmstat -S M 5’的三次数据报告。(vmstat命令是用来查看虚拟内存状况的,参数-S M表示以M为单位,5表示每5秒钟产生一次报告。)这里主要关注bi,bo和wa这三个值,bi代表每秒钟从硬盘读入数据的块数(因为硬盘是块设备),bo表示每秒钟写入硬盘数据的块数,wa表示CPU等待IO设备就绪的时间。
   
当值为0时:
来源:(http://blog.sina.com.cn/s/blog_5ce87d560100i31q.html) - [转载]Linux性能调优---虚拟内存_郭金鹏_新浪博客
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
  swpd    free    buff  cache    si    so      bi    bo    in    cs us sy id wa st
          1277        16    6340                              2 91  0
          1277        16    6340                  778 1203 3901  5 78 17  0
          1276        16    6340                  510 1094 3855  1 96  0
          1276        16    6340              13    294 1113 3896  3 89  0
          1276        16    6340              12    462 1122 3954  2 96  0
          1276        16    6340                      6 1027 3915  1 98  0
          1276        16    6340              19    982 1178 3894  2 79 19  0
          1276        16    6340              10    666 1195 3904  3 70 27  0
          1276        16    6340                    10 1031 3955  2 98  0
          1276        16    6340              15        9 1031 3902  5 95  0
          1275        16    6341                1420 1275 3930  2 67 32  0
          1275        16    6341                  290 1125 3939  1 89 10  0
          1275        16    6341              11      18 1029 3975  2 98  0
          1275        16    6341              26      22 1037 3984  1 98  0
          1275        16    6341                    18 1027 3892  2 97  0
          1275        16    6341                    10 1029 3878  1 98  0
          1275        16    6341                1106 1191 3935  1 81 18  0
          1275        16    6341              18  1121 1245 3870  1 61 38  0
          1275        16    6341              14    121 1086 3924  2 94  0
          1274        16    6341              12  1264 1245 3925  1 73 25  0
          1274        16    6342                  777 1208 3897  4 74 22  0
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
  swpd    free    buff  cache    si    so      bi      bo    in    cs us sy id wa st
          1274        16    6342                  267 1076 3870  5 95  0
          1274        16    6342                  391 1089 3856  2 98  0
          1274        16    6342              10        0 1033 3893  1 98  0
          1274        16    6342                      0 1026 3904  2 98  0
          1274        16    6342                1016 1193 3907  2 82 16  0
          1274        16    6342                  203 1092 3895  3 90  0
          1274        16    6342                1198 1243 3910  2 75 23  0
          1274        16    6342                    81 1062 3974  1 95  0
          1274        16    6342              14      13 1029 3907  1 98  0
          1274        16    6342              11  1231 1228 3960  4 71 24  0
          1274        16    6342              10    222 1102 3971  2 84 13  0
          1273        16    6343                      0 1032 3954  2 98  0
          1273        16    6343                1411 1314 4007  3 69 28  0
          1272        16    6343              14    386 1157 4162  2 91  0
          1272        16    6343              14    810 1170 3921  1 89 10  0
          1272        16    6343                  566 1137 3884  1 92  0
          1272        16    6343              10    265 1073 3938  2 98  0
          1272        16    6343              54    802 1196 4131  7 83  0
          1272        16    6344                      7 1046 3939  2 98  0
          1272        16    6344                  943 1206 3840  1 86 12  0
          1272        16    6344                    38 1033 3891  1 99  0
当值为60时:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
  swpd    free    buff  cache    si    so    bi    bo    in    cs us sy id wa st
          1268        17    6347                              2 91  0
          1267        17    6347                  526 1163 3875  2 92  0
          1267        17    6347                      4 1025 3934  1 98  0
          1267        17    6347                    17 1032 3917  1 98  0
          1267        17    6348                    34 1036 3909  2 98  0
          1267        17    6348                    16 1033 3990  1 98  0
          1267        17    6348                1172 1195 3936  3 83 14  0
          1266        17    6348              12    958 1252 3988  3 64 33  0
          1266        17    6348              32    372 1096 3875  1 97  0
          1266        17    6348                      4 1034 3838  1 98  0
          1266        17    6348              13      18 1030 3925  2 98  0
          1266        17    6348                1100 1200 3862  2 83 15  0
          1266        17    6348                  350 1139 3921  2 88 10  0
          1266        17    6348                      0 1028 3814  2 97  0
          1266        17    6349              10    769 1179 3886  2 83 15  0
          1266        17    6349                  411 1100 4004  1 96  0
          1266        17    6349              10    578 1118 3915  2 94  0
          1266        17    6349              26      85 1077 3865  1 92  0
          1266        17    6349                    18 1029 3879  1 98  0
          1265        17    6349              10    658 1116 3963  1 92  0
          1265        17    6349              10    798 1212 3901  3 71 27  0
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
  swpd    free    buff  cache    si    so      bi      bo    in    cs us sy id wa st
          1265        17    6349              10    245 1077 3900  1 98  0
          1264        17    6350              37      13 1044 3925  2 97  0
          1264        17    6350                  848 1187 3892  2 83 15  0
          1264        17    6350                  577 1119 3920  1 93  0
          1264        17    6350                    84 1062 3890  1 96  0
          1264        17    6350              10      18 1034 3824  2 97  0
          1264        17    6350                      0 1025 3931  1 98  0
          1264        17    6350              10    621 1110 3891  2 94  0
          1264        17    6350                  466 1144 3855  2 77 21  0
          1264        17    6350                      8 1031 3916  1 98  0
          1264        17    6350                    12 1028 3914  1 98  0
          1263        17    6351              10    929 1182 3962  3 85 12  0
          1263        17    6351              10  1089 1275 3988  2 58 40  0
          1263        17    6351              11    358 1142 3954  2 88 10  0
          1263        17    6351                      0 1026 3853  1 99  0
          1263        17    6351                      0 1029 3990  1 98  0
          1263        17    6351                    11 1029 3856  1 98  0
          1263        17    6351                1276 1204 3885  3 80 18  0
          1263        17    6351              14    514 1156 3849  2 77 21  0
          1251        17    6353            393    736 1234 4059  2 85 11  0
当值为100时:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
  swpd    free    buff  cache    si    so      bi    bo    in    cs us sy id wa st
          1202        18    6411                              2 91  0
          1201        18    6411              66  1153 1189 3604  3 45 53  0
          1200        18    6412            189    930 1226 8164  8 49 41  0
          1200        18    6412              44  1134 1261 3937  2 53 45  0
          1200        18    6413              98  1118 1229 3868  2 46 52  0
          1200        18    6413              45  1245 1251 3798  2 49 49  0
          1199        18    6413              74    922 1218 3853  2 53 45  0
          1198        18    6415            194  1084 1225 4919  4 51 44  0
          1198        18    6415              52    930 1192 3577  3 51 45  0
          1197        18    6416            147    831 1222 3995  0 13 51 36  0
          1197        18    6416              48  3240 1256 4557  5 46 48  0
          1196        18    6417              38  3730 1267 4536  5 53 42  0
          1195        18    6417              42  3336 1238 4048  3 51 45  0
          1195        18    6418              50  2759 1207 4237  5 52 42  0
          1195        18    6418              48  2201 1229 4138  4 41 54  0
          1194        19    6420              28  2360 1241 3913  2 53 44  0
          1193        19    6420              63  3812 1209 4344  5 46 48  0
          1193        19    6421            120  3942 1199 4452  5 43 52  0
          1192        19    6421              38  2963 1231 4272  4 45 51  0
          1191        19    6421              37  2784 1202 4373  5 49 46  0
          1191        19    6421              24  2558 1242 3909  2 54 43  0
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
  swpd    free    buff  cache    si    so      bi      bo    in    cs us sy id wa st
          1191        19    6422              36  1774 1212 4009  4 47 50  0
          1191        19    6422              29  4292 1208 4479  4 48 47  0
          1191        19    6422              44  3392 1204 4705  5 45 49  0
          1190        19    6423              56  3950 1250 4541  5 55 40  0
          1190        19    6423              67    877 1198 3515  2 54 44  0
          1189        19    6423              67    928 1201 3589  1 57 41  0
          1189        19    6424              69  1488 1190 3569  2 50 49  0
          1189        19    6424              61  2230 1228 3828  2 53 44  0
          1189        19    6424              30  2662 1275 4186  3 38 58  0
          1190        19    6424              22  1932 1274 4297  0 26 32 42  0
          1190        19    6425              56  1692 1289 4160  0 10 46 44  0
          1188        19    6427            412    795 1216 4795  0 13 56 31  0
          1188        19    6427            127  1854 1298 4400  0 13 52 35  0
          1187        19    6428              34  6174 1250 4808  0 11 45 44  0
          1187        19    6428            110  1285 1218 3507  1 52 46  0
          1185        19    6430            306    938 1215 3890  2 59 40  0
          1185        19    6431              52  3722 1207 4013  2 57 41  0
          1185        19    6431              17  9450 1205 5760  9 41 50  0
          1184        19    6431              16  7072 1188 4330  4 55 41  0
          1184        19    6431              20  7046 1241 5521  8 48 44  0
          1183        19    6432              46  7037 1190 4488  8 37 54  0

    发现什么了吗?很明显当值为100时,wa基本为50左右的值,这表示50%的CPU时间都在等待IO设备就绪(大好的CPU资源就这样被浪费了!)现在你明白瓶颈在哪里了吧?对,就是硬盘。说明我实验的这台机器硬盘IO的处理能力是最影响性能的了。那么该怎么解决呢?当然了,换个转速更快的硬盘当然可以,还有呢?增加内存有可能也可以。增加了内存以后,再把swappiness的值设小点,以减少硬盘IO的操作。内存够大时,无论页面文件的使用频率是高还是低都放在内存里,无须使用虚拟内存。

    但是在这个例子中,swpd的值始终为0,这表示没有虚拟文件被使用。这说明内存容量是足够的,即使再增加内存,作用也不大。最好的办法就是更换硬盘了。

    如何改变swapiness的值?你可以运行'echo 数值 > /proc/sys/vm/swapiness’ 或者 'sysctl –w vm.swappiness = 数值' 来修改内核中的实时参数。如果想机器在重启之后仍然保持这个数值的话,就需要在'/etc/sysctl.conf’文件中加上'vm.swappiness = 数值' 这一行。
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(268) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值