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 apply for a new ration card and what are the documents required?

List of documents required for new ration card application in Kerala and how to apply


Application for a new ration card should be addressed to Taluk Supply Officer (TSO) / City Rationing Officer (CRO) of applicant's residing area.

Primary document required are Residence certificate, Income certificate and incase the applicant's name is in another ration card then he/she should submit the reduction certificate ontained from previous TSO / CRO as proof for removing his/her name from old Ration card.

You can get the new application from your currently residing Taluk Supply Office.

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

Joomla and Forum Integration - Integrating Forums to Joomla

Joomla is one of the most popular CMS opensource packages. It is very easier to develop website's using Joomla. You just need to download Joomla package from Joomla's Official website www.joomla.org  and install it on your domain and later adding customizations to templates and feature and Your website is ready :). Now a days most websites provides a forum section for it users for discussing various article topics, gathering opinions etc.



Following are some best know forum opensource packages which can be integrated with Joomla and create a new forum experience for users


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