Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Memory overusage and functions : Allowed memory size exhaust

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Memory overusage and functions : Allowed memory size exhaust

Postby DaveOB1286 » Fri Sep 13, 2013 12:55 pm

I have a data processing script that I execute using a cron on a daily basis. The amount of data varies each day.

The data process code is broken down into separate files which helps ( me, anyway ) manage and maintain the code better.

The files are all run from a central script like this :

Code: Select all
for($q=1;$q<=30;$q++){
   include "/usr/www/users/xxx/function" . $q . ".php";
}


This is working very well for most the time, but every once in a while, I get an error :

PHP Fatal error: Allowed memory size of 67108864 bytes exhausted

So from my understanding, I have something that is chewing up the memory. I know that ultimately I would need to go through the code and find the cause, but in the mean-time, would the following be a solution :

To convert each of the separate 'include' files into a function, and then call the function. This way I think that all the memory used in each function would be unset at the end of each function, and the chances of an accumulated memory overload would be severely reduced.
There are only 2 variables that I need to carry over into each of the 'include' files.

So would the following work :

Code: Select all
$var1 = "Variable 1";
$var2 = "Variable 2";

for($q=1;$q<=30;$q++){
   if($q == 1){
      function MyFunction1(){
         global $var1;
         global $var2;
         include "/usr/www/users/xxx/function" . $q . ".php";
      }
      MyFunction1();  // call the function
      // at this point, all variables created in MyFunction1 will be unset ?
   }   

   // repeat the above IF() block for all 30 'include' files   
   
}
DaveOB1286
New php-forum User
New php-forum User
 
Posts: 1
Joined: Fri Sep 13, 2013 12:53 pm

Re: Memory overusage and functions : Allowed memory size exh

Postby johnj » Sun Sep 15, 2013 5:48 am

try using include_once() in your code, like this:

Code: Select all
for($q=1;$q<=30;$q++)
{
   include_once "/usr/www/users/xxx/function" . $q . ".php";
}
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron