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

时间:2021-08-14 15:24:15

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!

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

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.

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

Any suggestion on how to reduce the memory usage? Or is my only option to upgrade to 32GB of ram?

有关如何减少内存使用量的任何建议?或者我是升级到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

Biggest outputs from pmap -d

来自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

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`

2 个解决方案

#1


5  

I'll summarize what Lisa did to find the problem:

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

  • 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:
  • 使用pmap -d 检查单个PHP进程的内存布局。输出显示该进程使用了​​大量共享内存:
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:
  • 使用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
  • Disable eAccelerator in php.ini and remove the created shared memory regions:
  • 禁用php.ini中的eAccelerator并删除创建的共享内存区域:

for i in `ipcs -m | cut -d' ' -f2 | grep '^[0-9]'`; do ipcrm -m $i; done

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

#2


2  

Rasmus Lerdorf did a conference about PHP performance at Confoo in 2010 and he used a Wordpress blog as an example, this should give you great tools to answer your question:

Rasmus Lerdorf在2010年的Confoo上做了一个关于PHP性能的会议,他以一个Wordpress博客为例,这应该会为你提供很好的工具来回答你的问题:

http://talks.php.net/show/confoo10/1

http://talks.php.net/show/confoo10/1

To sum up:

总结一下:

  • Run a phpinfo() and disable PHP extensions that you don't use. They can take a lot of memory (imagick, curl, ...)
  • 运行phpinfo()并禁用不使用的PHP扩展。它们可以占用大量内存(想象力,卷曲......)
  • Generate a graph of your includes using the inclued.so extension. You might load useless functions in your wordpress setup.
  • 使用inclued.so扩展名生成包含图表。您可以在wordpress设置中加载无用的函数。
  • Run benchmarks with siege. Sometimes, tiny optimisations have great impact on performance, so make sure you have metrics, to help you make your decisions.
  • 用围攻运行基准测试。有时,微小的优化会对性能产生很大影响,因此请确保您有指标,以帮助您做出决策。
  • Use callgrind to show where you're loosing performance. In one of my project I was using md5() to hash my SQL queries and cache them. The md5() calls where using 20% of the CPU time.
  • 使用callgrind显示您失去性能的位置。在我的一个项目中,我使用md5()来散列我的SQL查询并缓存它们。 md5()调用使用20%CPU时间的地方。

I would definitely start by disabling PHP extensions if possible.

如果可能的话,我肯定会从禁用PHP扩展开始。

#1


5  

I'll summarize what Lisa did to find the problem:

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

  • 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:
  • 使用pmap -d 检查单个PHP进程的内存布局。输出显示该进程使用了​​大量共享内存:
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:
  • 使用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
  • Disable eAccelerator in php.ini and remove the created shared memory regions:
  • 禁用php.ini中的eAccelerator并删除创建的共享内存区域:

for i in `ipcs -m | cut -d' ' -f2 | grep '^[0-9]'`; do ipcrm -m $i; done

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

#2


2  

Rasmus Lerdorf did a conference about PHP performance at Confoo in 2010 and he used a Wordpress blog as an example, this should give you great tools to answer your question:

Rasmus Lerdorf在2010年的Confoo上做了一个关于PHP性能的会议,他以一个Wordpress博客为例,这应该会为你提供很好的工具来回答你的问题:

http://talks.php.net/show/confoo10/1

http://talks.php.net/show/confoo10/1

To sum up:

总结一下:

  • Run a phpinfo() and disable PHP extensions that you don't use. They can take a lot of memory (imagick, curl, ...)
  • 运行phpinfo()并禁用不使用的PHP扩展。它们可以占用大量内存(想象力,卷曲......)
  • Generate a graph of your includes using the inclued.so extension. You might load useless functions in your wordpress setup.
  • 使用inclued.so扩展名生成包含图表。您可以在wordpress设置中加载无用的函数。
  • Run benchmarks with siege. Sometimes, tiny optimisations have great impact on performance, so make sure you have metrics, to help you make your decisions.
  • 用围攻运行基准测试。有时,微小的优化会对性能产生很大影响,因此请确保您有指标,以帮助您做出决策。
  • Use callgrind to show where you're loosing performance. In one of my project I was using md5() to hash my SQL queries and cache them. The md5() calls where using 20% of the CPU time.
  • 使用callgrind显示您失去性能的位置。在我的一个项目中,我使用md5()来散列我的SQL查询并缓存它们。 md5()调用使用20%CPU时间的地方。

I would definitely start by disabling PHP extensions if possible.

如果可能的话,我肯定会从禁用PHP扩展开始。