如何优化php进程内存使用情况?

Lis*_*isa 10 php linux wordpress centos

我正在运行一个wordpress站点,每个PHP进程使用大约200mb到250mb的驻留大小内存.凭借16GB的ram,服务器只能处理大约70个进程.通过将虚拟内存增加到16GB,它可以处理140.之后负载不断上升.如果在10分钟内有200个连接,则3Ghz四核至强处理器上的服务器负载达到20!

我已经尝试停用所有插件,但这只会将每个进程的PHP内存使用量减少不到10%.suPHP告诉我哪个用户正在使用这么多内存,但不是wordpress代码的哪个部分.

有关如何减少内存使用量的任何建议?或者是我升级到32GB内存的唯一选择?

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
Run Code Online (Sandbox Code Playgroud)

来自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
Run Code Online (Sandbox Code Playgroud)

ipcs输出

------ 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`
Run Code Online (Sandbox Code Playgroud)

str*_*kol 7

我将总结Lisa为找到问题所做的工作:

  • 使用查看单个PHP进程的内存布局pmap -d <pid>.输出显示该进程使用了​​大量共享内存:
00002b3772850000 2097152 rw-s- 0000000000000000 000:00009   [ shmid=0x2d1b803a ]
Run Code Online (Sandbox Code Playgroud)
  • 检查共享内存区域ipcs -m.它表明用户nobody(Web服务器)创建了很多共享内存区域,这里只是其中的一小部分:
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
Run Code Online (Sandbox Code Playgroud)
  • 在php.ini中禁用eAccelerator并删除创建的共享内存区域:

我在`ipcs -m |中 cut -d'' - f2 | grep'^ [0-9]'`; 做ipcrm -m $ i; DONE