Sahal
Sahal

Reputation: 4146

PHP memory issue

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

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/

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

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

Upvotes: 0

Views: 669

Answers (2)

regilero
regilero

Reputation: 30536

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.

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.

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.

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).

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

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 easy solutions

  • 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

  • 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.

Upvotes: 1

ADW
ADW

Reputation: 4080

Changing the memory limit on PHP stops it being killed when it goes past a certain value. However, it does NOT physically give your hardware more memory (or swap). Ultimately, if it needs memory which you don't physically have then things will break.

Upvotes: 1

Related Questions