How can PHP cause memory leaks, buffer overflows, stack overflows and any other errors of such kind? Can PHP even cause such errors?
-
1Hopefully not. It should detect and avoid such errors before they happen. – Gumbo Mar 06 '10 at 10:40
-
2http://stackoverflow.com/questions/849549/detecting-memory-leaks-in-large-php-stacks – Pavunkumar Mar 06 '10 at 10:45
-
1Do you mean PHP the engine, or PHP scripts running on top of the engine? – Frank Shearar Mar 06 '10 at 12:00
-
@Frank php scripts running on top of the engine – yretuta Mar 07 '10 at 09:13
3 Answers
By causing some kinda infinite recursion, you can cause a PHP crash.
For example, a file that recursively requires itself should cause a stack overflow:
require __FILE__;
Or a recursion in the magic __sleep()
method that is supposed to unserialize an object, but calls serialize()
instead:
class sleepCrasher
{
public function __sleep()
{
serialize($this);
}
}
serialize(new sleepCrasher());
Or a class destructor that creates new instances:
class destructorCrasher
{
public function __destruct()
{
new destructorCrasher();
}
}
// Calling __destruct() manually is just for the sake of example,
// In real scenarios, PHP's garbage collector will crash PHP for you.
(new destructorCrasher())->__destruct();
As well as a recursive __toString()
:
class toStringCrasher
{
public function __tostring()
{
return strval($this);
}
}
strval(new toStringCrasher());
There are other recursion scenarios that PHP is protected against. For example, calling a recursive function without an exit condition or a recursive self-yielding generator. These ones do not cause a crash, but a Allowed memory size of ...
fatal error.
For more examples, you might want to see:

- 17,110
- 7
- 81
- 119
-
4
-
I hope for this guys worth this doesn't become the automatically accepted answer, because it hardly answers the question. Nice example though. – Humphrey Bogart Mar 15 '10 at 21:27
You can either do the stuff that would cause overflows in any language (like recursively calling the current function, mindlessly eating memory, etc.) or rely on the good old PHP interpreter to do that job. Just have a look at how many memory leaks were fixed in PHP5 (My favorite: In 5.2.6 they fixed bug #44069: 'Huge memory usage with concatenation using .
instead of .=
').
All in all PHP is ok (at most) if you just want to serve a single http request. But you can't really do sophisticated stuff with it (I once tried implementing a Peer2Peer client, the server died of memory shortage after just 10 minutes - could be a bug on my behalf of course, but I had spent several days finding leaks in my own code - to no avail).

- 73,842
- 19
- 118
- 155
PHP is a interpreted language, so all php scripts are protected from memory leaks, buffer overflow and stack overflow.
How ever you will encounter problems as such:
Integer overflow, if you assign a number too large it will overflow, and no exception will occur.
Out of memory, using more memory than memory size configured in your php.ini

- 924
- 1
- 8
- 17