Skip to main content

PHP Coding Practice - Memory Saving Tips

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

Comments

Popular posts from this blog

How to delete videos from your Youtube Watch History list?

How to Delete Individual or all videos from your Youtube Watch History list? Youtube keeps a fine record of the videos that you had watched earlier. You can view this by visiting the History section. If you want to remove the video's from the list do the following: Logon to Youtube and click on the "History" tab on the left menu to view Watch History ( Read more ) There will be check boxes corresponding to each video in the list Tick the check boxes of the videos which you want to remove Click on " Remove " button to delete the videos.

ICICI prudential Customer portal updated - Option to change password is missing - Know how to change your ICICI prudential password

Recently I received an SMS from ICICI prudential asking for login to their website's customer portal using the phone number as user Id and an autogenerated one time password given in the message as password. The SMS messsage was like this. Dear ***Cust Name*** login to your policy(ies) on www.iciciprulife.com with your user id as **mobile number*** and One time use password as ***password***

What are the Income Tax Rates for Indian citizens for Financial Year 2017-2018?

Income Tax Slab and Rates given below are for Indian citizens of age less than 60. This rates are applicable for the Financial Year 2017-2018 Income Tax Slab Rates Financial Year 2017-2018 Assessment Year 2018-19 Income Tax Slab Rates SLAB 1 Individuals whose total income not exceeding Rs. 2,50,000 ( 2.5 lakhs ) They are exempted from paying income tax.


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 »
Member
Search This Blog