原文:
有时候瞬时内存占用超了,就假死了... 这个没有什么办法的么? 我现在100mb空闲都会有时不时的假死。
那个啥,解决问题也是一个学习的过程,既然没人帮忙就自己搞了:
先看内存占用情况,Top 结果如下:
代码: 全选
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
14274 mysql 15 0 143m 20m 5944 S 0.0 4.0 0:01.73 mysqld
23722 www-data 15 0 36280 13m 3296 S 0.0 2.7 0:00.15 apache2
23767 www-data 15 0 36280 13m 3300 S 0.0 2.7 0:00.15 apache2
23713 www-data 15 0 36280 13m 3296 S 0.0 2.7 0:00.15 apache2
23712 www-data 15 0 36280 13m 3296 S 0.0 2.7 0:00.15 apache2
23744 www-data 15 0 34500 12m 3428 S 0.0 2.5 0:00.86 apache2
24463 www-data 15 0 34500 12m 3480 S 0.0 2.5 0:00.18 apache2
14087 bind 18 0 49484 9460 2048 S 0.0 1.8 0:00.01 named
22475 root 18 0 28232 6352 3400 S 0.0 1.2 0:00.00 apache2
23721 www-data 15 0 28592 4332 1156 S 0.0 0.8 0:00.00 apache2
24422 www-data 15 0 28572 4276 1112 S 0.0 0.8 0:00.00 apache2
24531 www-data 18 0 28232 4080 1076 S 0.0 0.8 0:00.00 apache2
24532 www-data 18 0 28232 4080 1076 S 0.0 0.8 0:00.00 apache2
22480 www-data 15 0 28112 4076 1080 S 0.0 0.8 0:00.00 apache2
22481 www-data 15 0 28112 4076 1080 S 0.0 0.8 0:00.00 apache2
23700 www-data 15 0 28232 4076 1068 S 0.0 0.8 0:00.00 apache2
24545 www-data 15 0 28232 4068 1064 S 0.0 0.8 0:00.00 apache2
24546 www-data 15 0 28232 4068 1064 S 0.0 0.8 0:00.00 apache2
24547 www-data 15 0 28232 4068 1064 S 0.0 0.8 0:00.00 apache2
22482 www-data 15 0 28112 4064 1068 S 0.0 0.8 0:00.00 apache2
23719 www-data 15 0 28232 4036 1040 S 0.0 0.8 0:00.00 apache2
23720 www-data 15 0 28232 4036 1040 S 0.0 0.8 0:00.00 apache2
24444 www-data 15 0 28232 4024 1036 S 0.0 0.8 0:00.00 apache2
24445 www-data 15 0 28232 4024 1036 S 0.0 0.8 0:00.00 apache2
24460 www-data 15 0 28232 4024 1036 S 0.0 0.8 0:00.00 apache2
24461 www-data 15 0 28232 4024 1036 S 0.0 0.8 0:00.00 apache2
24462 www-data 15 0 28232 4024 1036 S 0.0 0.8 0:00.00 apache2
24548 www-data 19 0 28232 3468 504 S 0.0 0.7 0:00.00 apache2
13659 root 16 0 8368 2652 2084 S 0.0 0.5 0:00.07 sshd
14110 root 15 0 4708 2364 1624 S 0.0 0.5 0:06.39 openvpn
13746 xyyhun 16 0 4980 2248 1464 S 0.0 0.4 0:00.08 bash
可以看出 Apache 和 Mysql 是内存大户。
Mysql 的 143mb 的 VIRT 很恐怖,但是看了网上资料后,发现优化的空间并不是很大,现在的数值大多数已经是考虑内存占用了。
有兴趣的可以看这里:
http://www.gzu521.com/it/website/mysql/200904/20784.htm
接着调查 apache:
主要参考这篇文章:
http://knlg.boofee.net/node/93
同时参考了Apache的文档:
http://www.phpchina.com/manual/apache/m ... uning.html
我的 VPS上默认的设置是这样的:
代码: 全选
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
我在重启Apache和刷新页面的同时,仔细的用Top观察内存的占用情况。发现每个线程都占用13MB左右的内存,按照这样的计算,原来的配置,我的峰值内存占用应该在2Gb左右,这个明显在我可怜的Vps承受范围之外,通过压力测试,我修改了配置:
代码: 全选
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 8
MaxClients 15
MaxRequestsPerChild 3000
</IfModule>
压力测试结果如下:
代码: 全选
top - 03:42:49 up 12 min, 1 user, load average: 5.47, 2.43, 1.06
Tasks: 35 total, 5 running, 28 sleeping, 2 stopped, 0 zombie
Cpu(s): 84.8%us, 6.1%sy, 0.0%ni, 9.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 524800k total, 502400k used, 22400k free, 0k buffers
Swap: 0k total, 0k used, 0k free, 0k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7994 www-data 16 0 39248 16m 3300 R 7.2 3.3 0:03.34 apache2
7743 www-data 15 0 41540 17m 3476 S 6.5 3.4 0:02.80 apache2
8027 www-data 15 0 35396 12m 3300 S 6.5 2.5 0:02.28 apache2
7741 www-data 16 0 35396 12m 3300 S 6.2 2.5 0:03.18 apache2
7742 www-data 15 0 35396 12m 3300 S 6.2 2.5 0:03.18 apache2
7972 www-data 16 0 39248 16m 3300 R 6.2 3.3 0:03.46 apache2
8016 www-data 16 0 41540 17m 3476 R 6.2 3.4 0:02.08 apache2
7741 www-data 15 0 35396 12m 3300 S 5.9 2.5 0:02.99 apache2
8017 www-data 16 0 48208 23m 3476 R 5.9 4.5 0:03.03 apache2
8019 www-data 15 0 35396 12m 3300 S 5.9 2.5 0:02.12 apache2
7742 www-data 15 0 35396 12m 3300 S 5.6 2.5 0:02.99 apache2
8018 www-data 16 0 34372 12m 3300 S 5.6 2.4 0:02.30 apache2
7744 www-data 15 0 34372 12m 3300 S 5.2 2.4 0:02.47 apache2
8029 www-data 15 0 34372 12m 3300 S 5.2 2.4 0:02.11 apache2
7743 www-data 15 0 39248 16m 3300 S 4.3 3.3 0:02.60 apache2
21827 mysql 15 0 145m 19m 5740 S 0.3 3.8 0:00.99 mysqld
1 root 15 0 2392 1344 1108 S 0.0 0.3 0:00.19 init
7734 root 18 0 28236 6356 3396 S 0.0 1.2 0:00.01 apache2
7766 xyyhun 15 0 2332 1088 876 R 0.0 0.2 0:00.02 top
21556 root 18 0 2392 572 340 T 0.0 0.1 0:00.00 init
21564 root 18 0 2084 864 684 S 0.0 0.2 0:00.00 cron
21581 root 22 0 2392 580 340 T 0.0 0.1 0:00.00 init
21616 syslog 15 0 1896 676 540 S 0.0 0.1 0:00.00 syslogd
21633 bind 18 0 48964 8940 2044 S 0.0 1.7 0:00.00 named
21661 root 18 0 4340 1092 736 S 0.0 0.2 0:00.00 openvpn
21673 root 15 0 5564 1076 672 S 0.0 0.2 0:00.00 sshd
21708 root 25 0 1744 532 444 S 0.0 0.1 0:00.00 mysqld_safe
21829 root 18 0 1660 560 488 S 0.0 0.1 0:00.00 logger
21834 root 16 0 8368 2640 2084 S 0.0 0.5 0:00.06 sshd
22056 root 18 0 8912 1748 1208 S 0.0 0.3 0:00.00 nmbd
22061 root 18 0 15856 2804 2068 S 0.0 0.5 0:00.00 smbd
22079 root 20 0 15856 1312 576 S 0.0 0.2 0:00.00 smbd
22264 xyyhun 15 0 8528 1600 1024 R 0.0 0.3 0:00.03 sshd
22270 xyyhun 16 0 4884 2144 1456 S 0.0 0.4 0:00.01 bash
23658 root 18 0 9248 1680 556 S 0.0 0.3 0:00.00 sendmail-mta
还剩20MB,好险 ... 会有瞬时用尽的情况,但是在测试间隔中可以恢复,测试完成后,马上就可以恢复到120mb左右的内存空闲。
代码: 全选
xyyhun@Carry:~$ ab -n 300 -c 20 -k http://xyyhun.fly-z.net/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xyyhun.fly-z.net (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Finished 300 requests
Server Software: Apache/2.2.12
Server Hostname: xyyhun.fly-z.net
Server Port: 80
Document Path: /
Document Length: 18410 bytes
Concurrency Level: 20
Time taken for tests: 60.315 seconds
Complete requests: 300
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 5624964 bytes
HTML transferred: 5548558 bytes
Requests per second: 4.97 [#/sec] (mean)
Time per request: 4021.011 [ms] (mean)
Time per request: 201.051 [ms] (mean, across all concurrent requests)
Transfer rate: 91.07 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 315 572 146.4 579 945
Processing: 1745 3341 1757.6 3053 15618
Waiting: 851 2090 1670.9 1840 14292
Total: 2065 3913 1713.8 3614 15949
Percentage of the requests served within a certain time (ms)
50% 3614
66% 3873
75% 4024
80% 4111
90% 4678
95% 5493
98% 10925
99% 15746
100% 15949 (longest request)
并发20个链接的情况下,需要60秒处理300个请求,90%的用户在5秒内得到页面,差强人意 ... 不过总比内存耗光到 ssh 都登不上的要好。
下面是温和点的测试:
代码: 全选
xyyhun@Carry:~$ ab -n 200 -c 5 -k http://xyyhun.fly-z.net/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xyyhun.fly-z.net (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software: Apache/2.2.12
Server Hostname: xyyhun.fly-z.net
Server Port: 80
Document Path: /
Document Length: 18410 bytes
Concurrency Level: 5
Time taken for tests: 82.461 seconds
Complete requests: 200
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 3732600 bytes
HTML transferred: 3682000 bytes
Requests per second: 2.43 [#/sec] (mean)
Time per request: 2061.526 [ms] (mean)
Time per request: 412.305 [ms] (mean, across all concurrent requests)
Transfer rate: 44.20 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 329 453 480.8 343 3543
Processing: 1174 1596 246.7 1616 2868
Waiting: 471 847 236.7 851 2145
Total: 1509 2049 543.0 2009 6287
Percentage of the requests served within a certain time (ms)
50% 2009
66% 2096
75% 2152
80% 2182
90% 2326
95% 2505
98% 4693
99% 4785
100% 6287 (longest request)
并发数为5,大部分的请求在2.5秒内处理掉,在80秒内处理了总共200个请求。大部分时间,内存剩余100MB左右。
另外有文章提到 Apache MPM worker 模式下,内存占用会比较少,有机会再测试吧,今天要睡了...