Php tips & tricks

Increase PHP memory limit

Sometimes you might get “Fatal error: Allowed memory size of …” PHP error. Usually this means that you run out of memory. It is recommended to double check your code and profile your app with xDebug. It should tell you which function is called how often and how much memory it eats. However, the code might be ok and in this case you just need to increase PHP memory limit.

memory_limit option sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.

Currently default memory_limit value is “128M”. You can change this value in several places.

php.ini

Edit php.ini is the recommended approach. Not all hostings allow you to edit it, for example shared hosting environments. You can contact your host to adjust it for you. To locate exact location of php.ini create simple php file and open it via browser.

and find Loaded Configuration File value.
php_ini_location

Edit the memory_limit parameter in the php.ini and Restart Apache.

.user.ini (PHP 5.3+)

PHP 5.3+ allows you to change the memory limit by placing a .user.ini file in the public_html folder. Create .user.ini file and add this line:

.htaccess

3. If you don’t have access to php.ini try adding this to an .htaccess file:

If none of the above works then talk to your host.

Difference between “Fatal error: Out of memory” and “Fatal error: Allowed memory size of …”

“Fatal error: Out of memory” instead of the more common “Fatal error: Allowed memory size of …” indicates the system cannot allocate more memory at all, meaning even internal functions cannot allocate more memory. Increase PHP memory limit does not help in this case. It is not your code issue. Apache had memory limits of its own and it affect PHP’s ability to allocate memory. Check httpd.conf for RLimitMEM parameter.

Increase PHP memory limit useful links

memory_limit directive description
PHP out of memory error even though memory_limit not reached

Leave a Reply

Your email address will not be published. Required fields are marked *