PHP is a nice language with features a wide range of automated features for memory but when it comes to mass processing for things like calculating pi or processing a database that's a couple dozen Gigabytes then you may run into a few troubles if things aren't done correctly. This of course only happens depending on what operating system you have and what updates you have done along with settings you have set but those pieces of information are a bit puzzling as some web servers have this problem while others don’t.
So this feature I'm going to talk about makes it impossible to process no more information than the amount of ram you have. So lets examine the code below and detect the bug in the following PHP code.
<?php
$b=pow(2,20);
for ($i=0;$i<1024;$i++) {
$a=bcpow('10',$b);
}
?>
Now if you examine that code it is a straight forward loop that does nothing. But it has great importance and I shall show you why. On the first line is the PHP opening tag as normal. Then second line is a variable assigned a number which is equivalent to 1MB in bytes. This will be used later. Then next line down is a loop and inside that loop is the assignment of a variable. Now it is the assignment of this variable that is interesting. The variable is assigned 1MB of data there by because the other variables are numbers the Ram usage should be about 1MB. However with that loop in place Ram usage for that thread will slowly crawl to 1GB (depending on your web server) until it's finished executing. So depending on your web server PHP does not delete the old data that it was previously but stores every variable that has ever been assigned within the computers Ram until the thread ends. And this can be a headache for processing lets say 2TB of data. So let’s see the work around for this.
Fortunately PHP has one function that can deal with this problem which is a big relief. This is the unset() function. The unset function should be used on all variables that are strings or arrays that are not being used again or are being reassigned especially long strings and large arrays. So the corrected version of the above script would be as follows:
<?php
$b=pow(2,20);
for ($i=0;$i<1024;$i++) {
unset($a); //this is added
$a=bcpow('10',$b);
}
?>
Also sometimes you might see the following
<?php
$b=pow(2,20);
for ($i=0;$i<1024;$i++) {
$a=''; //this is wrong and won't work
$a=bcpow('10',$b);
}
?>
Some people assign an empty string to unset a variable as laziness but if you ever need to unset a variable it is indeed best to use the unset() function due to the feature that is or has been embedded into PHP that can create extra Ram consumption. So as long as you use the unset() function appropriately you can execute a large script while using only small amounts of ram or in this case 1MB of Ram instead of the dreaded 1GB of Ram.