Jump to content

PHP memory question


guyfromfl

Recommended Posts

What happens if you do not unset an array before the script is done executing?

 

I am running through thousands of CSV files, parsing data for hundreds of thousands of customers.  It works fine for the first 5/6 hours then starts bogging down bad. 

 

I run about 5-10 CSVs per execution...I'm wondering if unsetting the arrays in the script would help this or not...I thought they would be unallocated after the script ends. Am I wrong?

Link to comment
Share on other sites

Yes, all data is deallocated when the script finishes, assuming you are using a standard setup.  If you're paranoid you can unset the arrays yourself, which can save memory during that script run.  For example:

 

$data = get_data();
$processed_data = process_data($data);
$more_processed_data = process_data_some_more($processed_data);

 

At the end of these 3 statements you have 3 arrays in memory.  You can save memory by unsetting the original arrays as soon as they are no longer needed (assuming they are not required later of course).  Unless a variable is overwritten (and therefore inaccessible), unset, or goes out of scope, php must keep it around in case it gets accessed later.

 

Another option when processing large volumes of data is to pass arrays by reference (this must be declared in the function definition) and modify them in-place.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.