PHP garbage collecting

Recently I was working on a PHP object that kept going over the allowed memory limit. The easy way around that would be to bump PHP’s memory limit. Unfortunately I planned on running the script every 5 seconds. I find bumping PHP’s memory limit okay in certain circumstances but I decided that I was just going to do it properly.

PHP’s garbage collecting can be a little slow. Using unset() does unset the variable but it still exists in memory until garbage collecting has occurred (which may not be fast enough). This is no good for what I wanted. What is written below seems to work well. By assigning a null value to the variable, we clear any data off of it.

1
2
$var = null;
unset($var);

Basic, I know. But it works awesome!