如何优化php进程的内存使用? [英] How to optimize the php process memory usage?
问题描述
我正在运行一个wordpress网站,每个PHP进程使用大约200mb到250mb常驻大小的内存。使用16GB的RAM,服务器只能处理大约70个进程。通过将虚拟内存增加到16GB,它可以处理140.之后,负载保持上升。如果在10分钟内有200个连接,则服务器负载在3Ghz四核至强处理器上达到20!
我已尝试停用所有插件,但这只会减少每个进程的PHP内存使用量小于10%。 suPHP告诉我哪个用户正在使用这么多内存,但不是什么部分的wordpress代码。
有关如何减少内存使用的任何建议?或者是我升级到32GB RAM的唯一选项吗?
PID用户公司NI VIRT RES SHR S%CPU%MEM TIME +
10585没人16 0 2266m 237m 199m S 21.3 1.5 1:09.17 / usr / bin / php
10597没人16 0 2257m 255m 226m S 15.3 1.6 0:17.56 / usr / bin / php
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输出
------ semaphore数组--------
键semid所有者允许nsems
0x000000a7 0 root 600 1
0x00000000 162529281没人600 1
0x00000000 162562050没人600 1
0x00000000 162594819没人600 1
0x00000000 162627588没人600 1
------消息队列--------
键msqid所有者允许使用的字节消息`
解决方案我将总结Lisa做了什么来找到问题:
- 使用
pmap -d
检查单个PHP进程的内存布局。输出结果表明该进程使用了大量的共享内存:
00002b3772850000 2097152 rw-s- 0000000000000000 000:00009 [shmid = 0x2d1b803a]
- 使用
ipcs -m
检查共享内存区域。它显示了由用户nobody(网络服务器)创建的很多共享内存区域,这里只是其中的一些:
< blockquote>
0x00000000 117964807 nobody 600 2147483648 1 dest
0x00000000 117997576 nobody 600 2147483648 1 dest
0x00000000 118030345没人600 2147483648 1 dest
0x00000000 118063114 nobody 600 2147483648 1 dest
- 在php.ini中禁用eAccelerator并删除创建的共享内存区域:
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屋!