最近,发现个人博客的Linux服务器,不停的出现内存报警的问题,尤其半夜睡着了,总是听到手机在响,所以说最好的办法就是关掉报警,继续睡觉,哈哈,开玩笑,不过问题总归是要解决的,这样就可以睡个好觉。
分析问题
发现问题以后,首先使用 free -m 指令查看当前服务器执行状况:
[root@VM-17-4-centos ~]# free -m total used free shared buff/cache available Mem: 1838 917 457 37 463 716 Swap: 0 0 0
可以看到我的服务器内存是2G的,但是目前可用内存只剩下457M,内存使用率高达90%。
继续看详细情况,使用 top -c指令:
top - 12:21:26 up 64 days, 17:14, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 122 total, 1 running, 121 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.7 us, 0.7 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1882752 total, 462248 free, 944976 used, 475528 buff/cache KiB Swap: 0 total, 0 free, 0 used. 728268 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 31069 mysql 20 0 1551996 573144 0 S 0.0 30.4 59:53.51 /usr/local/mysql/bin/mysqld --defaults-file=/data/+ 10305 root 20 0 1005744 53432 3104 S 0.0 2.8 59:04.81 /usr/local/qcloud/YunJing/YDEyes/YDService 19800 apache 20 0 509612 31624 19728 S 0.0 1.7 0:00.07 php-fpm: pool www 19782 apache 20 0 593004 31016 20388 S 0.0 1.6 0:00.08 php-fpm: pool www 19779 apache 20 0 583584 29344 19152 S 0.0 1.6 0:00.11 php-fpm: pool www 19781 apache 20 0 507264 28720 18944 S 0.0 1.5 0:00.10 php-fpm: pool www 19772 apache 20 0 507736 28460 18424 S 0.0 1.5 0:00.10 php-fpm: pool www 19775 apache 20 0 507736 28460 18424 S 0.0 1.5 0:00.10 php-fpm: pool www 19787 apache 20 0 507736 28460 18424 S 0.0 1.5 0:00.10 php-fpm: pool www 19769 apache 20 0 507736 28456 18420 S 0.0 1.5 0:00.10 php-fpm: pool www 19773 apache 20 0 507736 28456 18420 S 0.0 1.5 0:00.10 php-fpm: pool www 19774 apache 20 0 507736 28456 18420 S 0.0 1.5 0:00.10 php-fpm: pool www 19777 apache 20 0 507736 28456 18420 S 0.0 1.5 0:00.10 php-fpm: pool www 19780 apache 20 0 507736 28456 18420 S 0.0 1.5 0:00.10 php-fpm: pool www 19784 apache 20 0 507736 28456 18420 S 0.0 1.5 0:00.10 php-fpm: pool www
查看一下进程数量
[root@VM-17-4-centos ~]# ps -fe |grep "php-fpm"|grep "pool"|wc -l 35 [root@VM-17-4-centos ~]# ps -fe |grep "php-fpm"|grep "pool" apache 19767 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19768 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19769 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19770 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19771 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19772 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19773 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19774 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19775 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19776 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19777 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19778 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19779 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19780 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19781 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19782 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19783 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19784 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19785 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19786 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19787 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19788 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19789 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19790 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19792 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19793 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19794 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19795 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19796 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19797 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19798 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19799 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19800 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19801 926 0 11:48 ? 00:00:00 php-fpm: pool www apache 19802 926 0 11:48 ? 00:00:00 php-fpm: pool www
需要查看详细内存使用情况,是哪些进程耗费了这么多的内存呢?
使用指令:ps auxw|sort -rn -k4|head -40
查看消耗内存最多的前40个进程:
[root@VM-17-4-centos ~]# ps auxw|sort -rn -k4|head -40 mysql 31069 0.1 30.4 1551996 573144 ? Sl Jan24 59:53 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/3306/my3306.cnf root 10305 0.1 2.8 1005744 53048 ? Sl Jan29 59:04 /usr/local/qcloud/YunJing/YDEyes/YDService apache 19800 0.0 1.6 509612 31624 ? S 11:48 0:00 php-fpm: pool www apache 19782 0.0 1.6 593004 31016 ? S 11:48 0:00 php-fpm: pool www apache 19799 0.0 1.5 507736 28456 ? S 11:48 0:00 php-fpm: pool www apache 19788 0.0 1.5 507736 28452 ? S 11:48 0:00 php-fpm: pool www apache 19787 0.0 1.5 507736 28460 ? S 11:48 0:00 php-fpm: pool www apache 19784 0.0 1.5 507736 28456 ? S 11:48 0:00 php-fpm: pool www apache 19781 0.0 1.5 507264 28720 ? S 11:48 0:00 php-fpm: pool www apache 19780 0.0 1.5 507736 28456 ? S 11:48 0:00 php-fpm: pool www apache 19779 0.0 1.5 583584 29344 ? S 11:48 0:00 php-fpm: pool www apache 19777 0.0 1.5 507736 28456 ? S 11:48 0:00 php-fpm: pool www apache 19775 0.0 1.5 507736 28460 ? S 11:48 0:00 php-fpm: pool www apache 19774 0.0 1.5 507736 28456 ? S 11:48 0:00 php-fpm: pool www apache 19773 0.0 1.5 507736 28456 ? S 11:48 0:00 php-fpm: pool www apache 19772 0.0 1.5 507736 28460 ? S 11:48 0:00 php-fpm: pool www apache 19769 0.0 1.5 507736 28456 ? S 11:48 0:00 php-fpm: pool www apache 19798 0.0 1.4 506644 26880 ? S 11:48 0:00 php-fpm: pool www apache 19797 0.0 1.4 506644 26880 ? S 11:48 0:00 php-fpm: pool www apache 19790 0.0 1.4 506644 26876 ? S 11:48 0:00 php-fpm: pool www apache 19789 0.0 1.4 506644 26876 ? S 11:48 0:00 php-fpm: pool www apache 19786 0.0 1.4 506644 26884 ? S 11:48 0:00 php-fpm: pool www apache 19785 0.0 1.4 506876 28092 ? S 11:48 0:00 php-fpm: pool www apache 19783 0.0 1.4 506644 26880 ? S 11:48 0:00 php-fpm: pool www apache 19778 0.0 1.4 506628 26852 ? S 11:48 0:00 php-fpm: pool www apache 19776 0.0 1.4 506628 26852 ? S 11:48 0:00 php-fpm: pool www apache 19771 0.0 1.4 506644 26880 ? S 11:48 0:00 php-fpm: pool www apache 19770 0.0 1.4 506644 26880 ? S 11:48 0:00 php-fpm: pool www apache 19768 0.0 1.4 506644 26884 ? S 11:48 0:00 php-fpm: pool www apache 19767 0.0 1.4 506644 26884 ? S 11:48 0:00 php-fpm: pool www
查看第四列内存使用占比,发现除了mysql数据库服务之外,php-fpm服务池开启了太多子进程,占用超过大半内存,问题找到了,我们开始解决问题:设置控制php-fpm进程池进程数量。
解决问题
通过各种搜索手段,发现可以通过配置 pm.max_children 属性,控制php-fpm子进程数量,首先,打开php-fpm配置文件,执行指令:
vim /etc/php-fpm.d/www.conf
如图, pm.max_children 值为50,每一个进程占用1%-2.5%的内存,加起来就耗费大半内存了,所以我们需要将其值调小,这里将其设置为25;
同时,检查以下两个属性:
pm.max_spare_servers : 该值表示保证空闲进程数最大值,如果空闲进程大于此值,此进行清理 pm.min_spare_servers : 保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程;
这两个值均不能不能大于 pm.max_children 值,通常设置 pm.max_spare_servers 值为 pm.max_children值的60%-80%。
最后,重启php-fpm
[root@VM-17-4-centos ~]# systemctl restart php-fpm.service [root@VM-17-4-centos ~]# systemctl status php-fpm.service ● php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2021-02-19 12:28:55 CST; 3s ago Main PID: 26685 (php-fpm) Status: "Ready to handle connections" CGroup: /system.slice/php-fpm.service ├─26685 php-fpm: master process (/etc/php-fpm.conf) ├─26686 php-fpm: pool www ├─26687 php-fpm: pool www ├─26688 php-fpm: pool www ├─26689 php-fpm: pool www └─26690 php-fpm: pool www Feb 19 12:28:55 VM-17-4-centos systemd[1]: Starting The PHP FastCGI Process Manager... Feb 19 12:28:55 VM-17-4-centos systemd[1]: Started The PHP FastCGI Process Manager.
再次查看内存使用情况, 使用内存降低很多:
之后经过多次观察内存使用情况,发现此次改进后,服务器内存资源消耗得到很大缓解。
补充内容
查看php-fpm开启的进程数以及每个进程的内存限制
1.通过命令查看服务器上一共开了多少的 php-cgi 进程
[root@VM-17-4-centos ~]# ps -fe |grep "php-fpm"|grep "pool"|wc -l 6
2.查看已经有多少个php-cgi进程用来处理tcp请求
[root@VM-17-4-centos ~]# netstat -anp|grep "php-fpm"|grep "tcp"|grep "pool"|wc -l 0
3.linux+nginx+php环境中,每个php-fpm进程的内存限制
设置方法:
编辑php-fpm.conf配置文件
php_admin_value[memory_limit] = 128M(我服务器上的配置文件在/etc/php5/fpm/pool.d/www.conf 这个文件是被包含在php-fpm.conf里的) 后边的数字可以随便更改:32M,64M,128M,256M,512M,这个设置可根据你的服务器内存大小和你的需求来写,修改后要加载一下php-fpm服务。
参考连接
https://www.jb51.net/article/129528.htm