Fatal error: Allowed memory size of 8388608 bytes exhausted

PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y) in file.php

1. memory_limit globally from php.ini
memory_limit = 64M

2. memory_limit using .htaccess
php_value memory_limit 64M

3. memory_limit inside a php script.
ini_set(‘memory_limit’, ’64M’);

To change the memory limit for one specific script by including a line such as this at the top of the script:


The 12M sets the limit to 64 megabytes. If this doesn’t work, keep increasing the memory limit until your script fits.

You can change permanently for all PHP scripts running on the server by adding a line to the server’s php.ini file:

memory_limit = 64M

Keep in mind that a huge memory limit is a poor substitute for good coding. Some applications like data scrappers are run infrequently and require lots of memory like importing and processing a big data file using RSS, XML, JSON API etc.

Make sure each time script required server memory resources; need to release them when script/process finish first iteration to save memory uses 😉

PHP function might help….

1. unset — Unset a given variable

2. memory_get_usage — Returns the amount of memory allocated to PHP

3. memory_get_peak_usage — Returns the peak of memory allocated by PHP

4. __destruct your object references upon disposing of an object

5. The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

6. Turn PHP scripts into Linux daemons

7. Xdebug’s Profiler is a powerful tool that gives you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost.

class Memory
public $var='3.1415962654';

$baseMemory = memory_get_usage();

$a=new Memory;
echo sprintf('%8d: ',$i), memory_get_usage() - $baseMemory,"\n";

Fatal error: Allowed memory size of 8388608 bytes exhausted


Quality is not a product.. it is a process



PHP Debug Log – Trace Errors

Hello Friends,

I read some where “Quality is not a product.. it is a process 😉 ”

I think process is hard-work we do and product is final result we get.

But how we make our process to get a good product, as a LAMP developer I think track processes “Debug Log” is good sort of tool for monitoring programs.

Trouble shooting is quite simple with Log Files. Log file can be used with analysis tools, it’s possible to get a good idea of where errors are coming from, how often errors return.

Here you can see a simple PHP script to trace PHP programs…


function debugLog($log, $text)
$log_dir = dirname($log);
if( !file_exists($log_dir) or !is_dir($log_dir) or !is_writable($log_dir) )
return false;

$write_mode = ‘w’;
if( file_exists($log) && is_file($log) && is_writable($log) )
$write_mode = ‘a’;

if( !$handle = fopen($log, $write_mode) )
return false;

if( fwrite($handle, $text. “\n”) == FALSE )
return false;


Debugging Tools:


We can’t imagine processes without Log File 😉