A colleague of mine wrote a script that was exhausting the available memory. I narrowed it down to the following basic test case:
for ( $i = 0; $i <= 20; $i ++ ) {
echo memory_get_usage(). '<br />';
$Survey = new Survey( 14 );
echo memory_get_usage(). '<br /><br />';
}
exit('done');
This breaks on the third iteration:
3116696
49123440
49123440
95518368
95518368
[E_ERROR] Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes)
I managed to fix this, by simply adding an unset() call in the loop:
for ( $i = 0; $i <= 20; $i ++ ) {
echo memory_get_usage(). '<br />';
$Survey = new Survey( 14 );
unset( $Survey );
echo memory_get_usage(). '<br /><br />';
}
exit('done');
Now the script goes through its 20 iterations nice and smooth, with a relatively constant memory usage:
3116816
49123488
49123488
50691656
50691656
51088912
51088912
51079064
51079064
50535368
50535368
50809296
50809296
51033392
51033392
51157208
51157208
50543856
50543856
50892760
50892760
51045160
51045160
51132688
51132688
50535968
50535968
50968632
50968632
51058080
51058080
51143304
51143304
50562136
50562136
51067432
51067432
51067768
51067768
51170824
51170824
50551712
done
This confuses me! Isn’t the garbage collector supposed to clean up the object, as it’s variable has been overwritten? I’m running PHP 5.3, so circular references can’t be the cause of this problem.
Circular references can still be a problem in 5.3:
There is probably also some memory-hogging resource inside
Surveythat takes up all of this memory; the observed behavior should be a combination of a ref cycle and such a resource.What’s in
Surveyexactly?