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

Payback Points - How to redeem - How to merge multiple payback accounts - Block Payback card - Payback customer care

Your SBI Debit card ending with XX0000 is deactivated only for Internet txn.

SBI account holders may have received an SMS with following message, supposed to be from State Bank of India (SBI).

Your SBI Debit card ending with XX0000  is deactivated only for Internet txn. To activate send SMS "SWON ECOM 0000" to 09223966666. No change for ATM/POS usage
** Replace the four Zeros with last 4 digits of your debit card number

Recently many of the SBI account holder has losed their money due to a hi-tech ATM robbery which happened in Thiruvananthapuram, capital city of Kerala.

How to turn off BSNL Buzz services - Steps to deactivate BSNL Buzz messages

BSNL Buzz is a service from BSNL for mobile customers, in which when your phone is in idle mode, teasers or small buzz messages related to topics like entertainment, lifestyle, sports, business, fun, polls, daily news updates are delivered to your mobile. When you activate a BSNL SIM card, through which BSNL BUZZ is available, you automatically accept the terms and conditions for teaser delivery.

Recently, I bought a new BSNL SIM card. After the activation, when ever I unlock the phone, I was getting lots of buzz message one after another. I have to click on cancel / back button multiple time to view the home screen. Some times when some call arrives, it was difficult to attend calls due to this teaser popups. So I decided to deactivate the BSNL buzz. Here are the steps to Deactivate or Turn Off BSNL Buzz.


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