如何优化php进程内存使用? [英] How to optimize the php process memory usage?
问题描述
我正在运行一个 wordpress 站点,每个 PHP 进程使用大约 200mb 到 250mb 的常驻大小内存.使用 16GB 内存,服务器只能处理大约 70 个进程.通过将虚拟内存增加到 16GB,它可以处理 140.之后负载不断上升.如果在 10 分钟内有 200 个连接,则服务器负载在 3Ghz 四核至强处理器上达到 20!
我尝试过停用所有插件,但这只会使每个进程的 PHP 内存使用量减少不到 10%.suPHP 会告诉我哪个用户使用了这么多内存,但不会告诉我 wordpress 代码的哪一部分.
关于如何减少内存使用的任何建议?或者是我升级到 32GB 内存的唯一选择?
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND10585 没有人 16 0 2266m 237m 199m S 21.3 1.5 1:09.17/usr/bin/php10597 没有人 16 0 2257m 255m 226m S 15.3 1.6 0:17.56/usr/bin/php
来自 pmap -d 的最大输出
000000000e8b8000 27580 rw--- 000000000e8b8000 000:00000 [匿名]00002b3772850000 2097152 rw-s-0000000000000000 000:00009 [ shmid=0x2d1b803a ]00002b37f2a62000 55108 r---- 0000000000000000 0fd:00000 语言环境存档映射:2320852K 可写/私有:30012K 共享:2097152K
ipcs 输出
------ 信号量数组------密钥 semid 所有者 perms nsems0x000000a7 0 根 600 10x00000000 162529281 没有人 600 10x00000000 162562050 没有人 600 10x00000000 162594819 没有人 600 10x00000000 162627588 没有人 600 1------ 消息队列------关键 msqid 所有者允许使用的字节消息`
我将总结 Lisa 为找到问题所做的工作:
- 使用
pmap -d
检查单个 PHP 进程的内存布局.输出显示进程使用了大量共享内存:
00002b3772850000 2097152 rw-s- 0000000000000000 000:00009 [ shmid=0x2d1b803a ]
- 使用
ipcs -m
检查共享内存区域.它表明有很多由用户 nobody(Web 服务器)创建的共享内存区域,这里只是其中的几个:
0x00000000 117964807 没人 600 2147483648 1 dest0x00000000 117997576 没有人 600 2147483648 1 目标0x00000000 118030345 没有人 600 2147483648 1 目标0x00000000 118063114 没有人 600 2147483648 1 目标
- 在 php.ini 中禁用 eAccelerator 并删除创建的共享内存区域:
for i in `ipcs -m |剪切 -d' ' -f2 |grep '^[0-9]'`;做ipcrm -m $i;完成
I am running a wordpress site and each PHP process usage about 200mb to 250mb resident size memory. With 16GB of ram, the server can only handle about 70 processes. By increasing the virtual memory to 16GB it can handle 140. After that the load keeps rising. If there are 200 connections in 10 minutes, the server load reaches 20 on a 3Ghz quad-core xeon processor!
I have tried deactivating all the plugins, but this only reduces the PHP memory usage of each process by less than 10%. suPHP tells me which user is using so much memory, but not what part of the wordpress code.
Any suggestion on how to reduce the memory usage? Or is my only option to upgrade to 32GB of ram?
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10585 nobody 16 0 2266m 237m 199m S 21.3 1.5 1:09.17 /usr/bin/php
10597 nobody 16 0 2257m 255m 226m S 15.3 1.6 0:17.56 /usr/bin/php
Biggest outputs from pmap -d
000000000e8b8000 27580 rw--- 000000000e8b8000 000:00000 [ anon ]
00002b3772850000 2097152 rw-s- 0000000000000000 000:00009 [ shmid=0x2d1b803a ]
00002b37f2a62000 55108 r---- 0000000000000000 0fd:00000 locale-archive
mapped: 2320852K writeable/private: 30012K shared: 2097152K
ipcs output
------ Semaphore Arrays --------
key semid owner perms nsems
0x000000a7 0 root 600 1
0x00000000 162529281 nobody 600 1
0x00000000 162562050 nobody 600 1
0x00000000 162594819 nobody 600 1
0x00000000 162627588 nobody 600 1
------ Message Queues --------
key msqid owner perms used-bytes messages`
I'll summarize what Lisa did to find the problem:
- Check the memory layout of a single PHP process with
pmap -d <pid>
. The output showed that there's a huge amount of shared memory used by the process:
00002b3772850000 2097152 rw-s- 0000000000000000 000:00009 [ shmid=0x2d1b803a ]
- Examine the shared memory regions with
ipcs -m
. It showed that there are a lot of shared memory regions created by user nobody (the web server), here are just a few of them:
0x00000000 117964807 nobody 600 2147483648 1 dest 0x00000000 117997576 nobody 600 2147483648 1 dest 0x00000000 118030345 nobody 600 2147483648 1 dest 0x00000000 118063114 nobody 600 2147483648 1 dest
- Disable eAccelerator in php.ini and remove the created shared memory regions:
for i in `ipcs -m | cut -d' ' -f2 | grep '^[0-9]'`; do ipcrm -m $i; done
这篇关于如何优化php进程内存使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!