My colleague wrote a script that ran out of available memory. I narrowed it down to the following basic test:
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 in 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 the unset() call to 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 20 iterations nice and smooth, with relatively constant use of memory:
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
It bothers me! Shouldn't the garbage collector clear the object since the variable was overwritten? I am running PHP 5.3, so circular links cannot be the cause of this problem.
source share