How to Avoid PHP Object Nesting/Creation Limit?

Posted by Will Shaver on Stack Overflow See other posts from Stack Overflow or by Will Shaver
Published on 2010-04-06T00:29:29Z Indexed on 2010/04/06 0:33 UTC
Read the original article Hit count: 320

Filed under:
|
|

I've got a handmade ORM in PHP that seems to be bumping up against an object limit and causing php to crash. Here's a simple script that will cause crashes:

<?
class Bob
{
    protected $parent;  
    public function Bob($parent)
    {
        $this->parent = $parent;
    }

    public function __toString()
    {
        if($this->parent)
            return (string) "x " . $this->parent;
        return "top";
    }
}


$bobs = array();
for($i = 1; $i < 40000; $i++)
{
    $bobs[] = new Bob($bobs[$1 -1]);
}    
?>

Even running this from the command line will cause issues. Some boxes take more than 40,000 objects. I've tried it on Linux/Appache (fail) but my app runs on IIS/FastCGI. On FastCGI this causes the famous "The FastCGI process exited unexpectedly" error.

Obviously 20k objects is a bit high, but it crashes with far fewer objects if they have data and nested complexity.

Fast CGI isn't the issue - I've tried running it from the command line. I've tried setting the memory to something really high - 6,000MB and to something really low - 24MB. If I set it low enough I'll get the "allocated memory size xxx bytes exhausted" error.

I'm thinking that it has to do with the number of functions that are called - some kind of nesting prevention. I didn't think that my ORM's nesting was that complicated but perhaps it is. I've got some pretty clear cases where if I load just ONE more object it dies, but loads in under 3 seconds if it works.

© Stack Overflow or respective owner

Related posts about php

Related posts about objects