3

I have a PHP program that will run forever (not a webpage a socket server). After processing over 1000 requests the program eventually crashes due to an out of memory exception.

Here is a link to my project.

Here is a link to my program.

I am not sure why this happens, I have tried using garbage collection functions in the function that processes requests (onMessage) in the program but it does not result in any changes. Any suggestions would be appreciated.

2
  • 3
    Check your code for memory leaks. They may not be easy to find. PHP typically doesn't run as a daemon so you might run into unexpected situations. At the very least make it so you can stop/start/respawn your daemon. Commented Jan 3, 2014 at 16:53
  • 1. Check the memory usage at specific points of your programe 2. increase the memory_limit(); 3. check wether the garbage collector is enabled Commented Jan 3, 2014 at 17:04

1 Answer 1

3

Investing huge amounts of effort, you may be able to mitigate this for a while. But in the end you will have trouble running a non-terminating PHP application.

Check out PHP is meant to die. This article discusses PHP's memory handling (among other things) and specifically focuses on why all long-running PHP processes eventually fail. Some excerpts:

There’s several issues that just make PHP the wrong tool for this. Remember, PHP will die, no matter how hard you try. First and foremost, there’s the issue of memory leaks. PHP never cared to free memory once it’s not used anymore, because everything will be freed at the end — by dying. In a continually-running process, that will slowly keep increasing the allocated memory (which is, in fact, wasted memory), until reaching PHP’s memory_limit value and killing your process without a warning. You did nothing wrong, except expecting the process to live forever. Under load, replace the “slowly” part for "pretty quickly".

There’s been improvements in the “don’t waste memory” front. Sadly, they’re not enough. As things get complex or the load increases, it’ll crash.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Chris, I guess I will have to use a more reliable language.
What about the PHP garbage collector?
@DanFromGermany, there are other challenges to be faced when running PHP as a daemon, some of which are outlined in that same article. I'm not saying it's impossible (the ReactPHP guys have had moderate success) but it's very difficult.
I've been going through the article(s), I don't really agree,.. PHP is not ment to be used as extreme daemons but they work, with gc collector, forks/process control (pcntl). Other servers and games do maintenance too, every week a restart or something, its the same with all languages, not only PHP. You can't allocate more memory than available so you have to deal with it, in any language. My opinion :)
@DanFromGermany "You can't allocate more memory than available so you have to deal with it, in any language." Very true, but some languages are better at this than others. PHP is not a great language for long-running processes. My opinion :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.