Reducing wild Ram consumption with PHP

Updated cwarn23 4 Tallied Votes 676 Views Share

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.

happygeek commented: thanks for the tutorial +12
Member Avatar for LastMitch
LastMitch

@cwarn23

I don't know how this works?

I kept getting this error:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\EasyPHP-12.1\www\4.php on line 5

I just want to see how this works.

I attached how it looks like on my system:

251

Gaetane 12 Newbie Poster

To help anyone else with this problem:> It depends which version of the code you were trying and on other variables like (not sure, not really my field of expertise) CPU usage at the time, CPU capabilities, RAM available, etc. What PHP is telling you is that it was not able to finish executing the code because it ran out of time.

In the php.ini you will find max_execution_time which is set in seconds. Now if you have ever coded yourself into endless loop (everyone should try it at least once), you will greatly appreciate this feature ^_^. You might need to up this time limit to run this particular code. Edit the php.ini and restart your Apache server.

To edit it at runtime use:

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

Sources:
http://davidwalsh.name/increase-php-script-execution-time-limit-ini_set
http://stackoverflow.com/questions/4306605/is-ini-setmax-execution-time-0-a-bad-idea

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.