Home   Best Sellers   Blogging   Coding & Design   Technology   SEO   Travel & living   Career   Videos   Tips   Calculators     
Home  »     »  PHP Coding Practice - Memory Saving Tips

PHP Coding Practice - Memory Saving Tips

Tuesday, October 18, 2011

Here are some tips you can try to prevent the "PHP script running Out of memory" Problem.

Poor programming techniques or lack of knowledge in what affects memory can end up with a situation like script running out of memory

The error is something like "Fatal error: Out of memory (allocated 45613056 bytes exhausted) "

Few things you can do

1. Check which part of your code  using the most amount of memory.

PHP function for this :

//--------Code Start------
memory_get_peak_usage();
//-------Code End-------

This function will return the current memory usage at the point it is executed. You'll eventually see where your script is using the most amount of memory


2. Destruct your object references upon disposing of an object

__destruct method to your objects which unsets all references to other objects.

//--------Code Start------

protected function __destruct()
{
  $this->childObject = null;
}

//--------Code End------



3. Use functions where possible

After executing a function, PHP clears the memory it was using, at least more efficiently than if not using a function. When using recursive code this will save a lot of memory.


4. Database usage

Database access is expensive. We can try file system to store data. Accessing File system ia less complex and memory consuming than accessing DB.


5. Cache your filesystem checks

While executing a script we regularly check for a file or directory. for example, suppose we have an include script and we have to include in different locations of the page. So in this case is we use file_exists()  function each time before include that script, it will cost some memory..

//--------Code Start------
- - - - -
- - - - -
if( file_exists("include/login.php") )
  include("include/login.php");
- - - - -
- - - - -

//--------Code End------



In this case instead of using file_exists() each time, store the file paths you've already checked in an array (Or object property) and use in_array().

We can cache the file check using an array and then by using in_array() function we can determine whether the checking has already done.

//--------Code Start------
if(!in_array($file_path, $this->file_path_list))
{
  // ... Your code comes here
}
//--------Code Start------


6. Reassign NULL value to variables along with unsetting them

After using the variable assign it will null.

$var = null;

or you can use unset() function

unset($var);

The unset(); function is useful when the garbage collector is doing its rounds however until then the unset(); function simply destroys the variable reference to the data, the data still exists in memory and PHP sees the memory as inuse despite no longer having a pointer to it. The solution: Assign null to your variables to clear the data, at least until the garbage collector gets ahold of it.


7. Do not use objects if not needed


Objects add extra overhead for your scripts

- Use arrays, If not writing to a database
- Use objects, If you will be modifying data in a database.

Source



How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
PHP Coding Practice - Memory Saving Tips.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »