Friday, January 22, 2016

Joomla: Fix "Allowed memory size exhausted" PHP error (increase memory_limit safely)

I hosted my website on a Linux server. While I accessed the Contact us page of the site I got the error below:

"Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php"

The error message included the full path to the PHP file, for example: public_html//plugins/system/xyz.php and the line number where the error occurred (in my case it was line 79).

What this error means

PHP enforces a memory limit per process (memory_limit). The message "Allowed memory size of 33554432 bytes exhausted" means a PHP script tried to use more than the configured limit (here 33554432 bytes = 32M).

Common causes:

  • Large data processing (big arrays, large file handling, image processing).
  • A plugin or extension requiring more memory.
  • A bug or infinite loop that consumes memory.
  • A default server-level limit (especially on shared hosting).

Quick, safe fixes (in recommended order)

1) Diagnose first

  • Check the file and the reported line number (e.g., public_html//plugins/system/xyz.php line 79). Look for obvious loops, large allocations or file reads.
  • Temporarily enable error reporting or check logs for fuller stack traces.
  • Use phpinfo() (create a small PHP file with ) to see the configured memory_limit.

2) Increase memory for a single script (temporary)

If you need a quick temporary increase for debugging, you can add an ini_set call near the top of the script that triggers the error. Example (original author snippet):

ini_set('memory_limit', '-1');

Warning: iniset('memorylimit', '-1') removes the limit entirely. This is not recommended on production systems because it can allow runaway processes to consume all server RAM. Prefer setting a specific larger limit, for example:

ini_set('memory_limit', '128M');

Place the iniset line just before the memory-intensive operation or near the top of the plugin file reported in the error (for example, just above line 79 in publichtml//plugins/system/xyz.php). This change affects only the running PHP process executing that file.

3) Make a permanent server-level change (preferred when you control the server)

  • php.ini (global change):

Open your php.ini and change or add:

memory_limit = 128M

Restart the web server (apache/nginx + php-fpm) after changing php.ini.

  • .htaccess (if PHP runs as an Apache module and your host allows it):
php_value memory_limit 128M
  • Joomla configuration (depending on setup):

If you cannot change system files, you can try adding iniset calls in a central place that runs early (but be cautious). On some hosts iniset is disabled.

4) Contact your hosting provider

If you are on shared hosting, the host may enforce a hard limit. Contact support to request a memory increase or upgrade your plan.

Diagnostic steps to avoid simply raising the limit

  • Review the plugin code (here: plugins/system/xyz.php). Try disabling the plugin temporarily to confirm it is the source.
  • Search for large data structures or excessive recursion.
  • Update the extension or check for known bugs.
  • If the operation is intended (e.g., processing very large files), consider reworking the code to use streaming, chunked processing, or temporary files instead of keeping everything in memory.

Example: where to insert ini_set in a plugin file

Open the PHP file referenced in the error (e.g., publichtml//plugins/system/xyz.php) and add an iniset line near the top, before the memory-intensive code runs. For example:

<?php
// plugins/system/xyz.php

// Temporary increase - prefer specific value over -1
ini_set('memory_limit', '128M');

// rest of the plugin code ...

Again: prefer a specific limit (e.g., 128M) over '-1'. Use '-1' only for controlled debugging on a development machine.

Safety and rollback

  • Always make a backup of any file before editing.
  • Test changes on a development or staging site first.
  • If you added ini_set and the page works, use that as a temporary measure while you find the root cause. Remove or reduce the override once you address the memory usage.

Summary checklist

  • Verify file and line number from the error message.
  • Inspect code for loops or big allocations.
  • Try a temporary iniset('memorylimit', '128M') if you control the environment.
  • Prefer changing php.ini or .htaccess for permanent needs.
  • Contact your host if limits are enforced at the server level.
  • Fix the root cause (optimize code, update plugin) rather than permanently removing limits.

That's it. Happy Coding

No comments: