views:

490

answers:

2

So I have a very odd issue.

First, I'm using PHP 5.2.6 (as part of MAMP, though this also happens on a LAMP server running 5.2.6 as well), writing a web site using Zend Framework 1.7.2.

Second, the software I'm writing does a rather complicated statistical calculation that requires a lot of memory. Usually it runs fine with 128MB set as the memory limit.

So here's the fun part.

I have a class which is exclusively responsible for getting the data it needs in, doing the calculations required, then returning the values. After it runs, I have been unsetting it, as PHP seems to think that it's still in scope and holding up the memory for it, despite finishing the function in which it is called (this was causing problems where the second run of the calculation was hitting the memory limit and the script died). After this calculation is run once inside that function, it is run again in another function to do another calculation (this is during the same run of execution). Here's where things get weird.

If I run that second calculation, the calling function runs again, despite the fact that there are absolutely no calls to that function from within the code which does this calculation. Also, no errors or warnings appear in PHP.

However, if I increase the memory limit (say, to 1024 MB), it doesn't happen. The overall calling function runs once and continues along its merry way.

So, let me draw a map below.

function A{ calls function B calls function D }

function B{ calls calculation function C }

function D{ calls calculation function C }

with 128 MB of memory, path of functions is as follows (in and back to the caller): A->B->C->B->A->D->C->D->then it runs A from the start of the function, for some reason, to the end (there's a conditional case which becomes true during this execution path, so the path doesn't repeat)

with 1GB of memory, path of functions is as follows: A->B->C->B->A->D->C->D->end of A, which is what is supposed to happen.

If I remove the second calculation from function D, it works. If I up the memory limit, it works.

Question is: have I found a bug in PHP memory management? Or perhaps in the Zend Framework? Again; by increasing the memory limit per script in php.ini, I don't see this issue. That, or remove the second instance of running this calculation which uses a lot of memory.

I'm very, very sure this is the way it happens, despite the oddness of the way it does (I've never seen this kind of jumping back to the start of a calling function). Usually what would happen in this case is that PHP would error out and die, not restart the calling function.

A: 

Firstly, turn on all error reporting, including E_STRICT.

I would suspect that the low memory causes an execution path you weren't expecting, and maybe additional error output will shed more light.

Paul Dixon
See comments in the question.
jedcred
+1  A: 

Have you verified the program flow using debug_backtrace()? I thinks it's more probable that your php code is the cause of this than the zend engine. I would recommend putting a dump of the trace into a file at the start of each function and analysing that first:

function myFunction() {
    file_put_contents(print_r(debug_backtrace(), true) . "\n--\n", 'trace.txt');
    //...
soulmerge
Will give that a shot.
jedcred