PHP内存问题 [英] PHP memory issue

查看:122
本文介绍了PHP内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置 memory_limit的为-1 。我还是让我的内存不足的问题。

I set memory_limit to -1 . Still i am getting out of memory issues.

我与遗留系统,这是不好codeD(:))工作。我跑apache的基准来检查系统中的并发用户访问

I am working with a legacy system, which is poorly coded ( :) ). I ran apache benchmark to check the concurrent user access to the system

AB -n2000 -c100 HTTP://......com/

ab -n2000 -c100 http://......com/

在日志文件中我看到这么多的内存相关的问题。

In the log file i see so many memory related issues.

在code,他们使用对象的缓冲。这可以是该问题?为对象缓冲是与 memory_limit的

In the code they use object buffering. This can be the issue ?. Is object buffering is related to memory_limit ?

推荐答案

在PHP对象缓冲:我不知道这意味着什么,如果你的意思是输出缓冲带ob_start和ob_stop它不是有关对象缓存和还没有真正对PHP的内存使用情况。

Object buffering in PHP : I don't know what it means, if you mean Output buffering with ob_start and ob_stop it is not related to object buffering and has not really an impact on memory usage of PHP.

PHP中的内存使用取决于创建的对象的大小,同时在构建请求的响应。的如果执行多次相同的请求每个PHP执行的内存使用量应该是一样的

Memory usage of PHP depends on the size of created objects while you build the response of the request. If you perform several times the same request the memory usage of each php execution should be the same.

随着内存使用情况,你唯一要做的就是避免因为太多的内存使用情况的要求崩溃了没有限制。这意味着,如果你的问题是你的索引页的内存使用情况,你可以很容易地在此设置设置一些值来测试它,并且递减,直到崩溃(64Mo,32Mo,16开本,8Mo等)。你不需要AB为。

With a 'no limit' on memory usage the only thing you do is avoiding a request crash because of too much memory usage. That mean if your problem is memory usage on your index page you can easily test it by setting some values in this setting, and decrease until it crash (64Mo, 32Mo, 16Mo, 8Mo, etc). You do not need ab for that.

现在,当你使用AB您对几个并行请求Apache服务器响应。对于每一个PHP请求,你有一个新的Apache进程创建。而这个新的Apache进程将执行一个独立的PHP的东西,因为其他情况过程做同样的事情将采取相同的内存量(当你请求同样的页面,并没有什么不同的PHP执行之间共享,每个PHP执行于一体的Apache进程完成)。

Now, when you're using ab you make your apache server respond to several parallel requests. For each PHP request you have a new apache process created. And this new apache process will execute an independant PHP-thing, and it will take the same amount of memory as the others process doing the same thing (as you request the same page, and nothing is shared between different PHP execution, and each PHP execution is done in one apache process).

我假设你使用的是与mpm_ prefork和mod_php,并且没有任何PHP-FPM或FastCGI的PHP的Apache。

I assume you're using apache with mpm_prefork and mod_php, not any php-fpm or fastcgi php.

所以,如果你在这种情况下有一个内存问题的这也许您允许的Apache 太多的过程。默认情况下它是150,如果每个过程需要的RAM 30MB(检查与顶级),那么它使30 * 150 = 4.3Go。看到这个问题?

So If you have a memory problem in that situation it's maybe that you allow too much process for apache. By default it's 150, if each process takes 30Mb of RAM (check that with top) then it makes 30*150=4.3Go. See the problem?

3个简单的解决方案


  • 减少Apache进程的(MaxClients的)的数量,并设置MinSpareServer,MaxSpareServer和startserver的,以相同的金额,你不会松动的时间来创建和销毁Apache进程。

  • 限制了PHP应用程序的内存使用,然后你就可以处理更多的过程(当然,不是那么容易的,可长时间重写)

  • 使用的 APC ,它降低了内存使用(和加快执行)

  • decrease the number of apache process (MaxClients), and set the MinSpareServer, MaxSpareServer and StartServer to that same amount, you wont loose time creating and destroying apache processes.
  • limit the PHP application memory usage, then you'll be able to handle more process (well, not so easy, can be a long rewrite)
  • use APC, it decrease the memory usage (and speed up execution)

和之后的其他解决方案更为复杂

and after that the other solutions are more complex


  • 使用中的工作人员模式或nginx的一个apache,并让PHP从Web服务器与 PHP-FPM

  • 使用代理缓存例如清漆,以赶上可以被缓存的(伪静态内容)的请求,并避免要求阿帕奇&安培; PHP太多了。

  • use an apache in worker mode or nginx, and get php out of the webserver with php-fpm
  • use a proxy cache like varnish to catch requests that can be cached (pseudo static content), and avoid requesting apache & PHP too much.

这篇关于PHP内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆