Solving PHP error – Allowed memory size of xxxx bytes exhausted

If you see the following error while executing a php script, that means the execution is trying to use more memory then the maximum amount of memory a script may consume.

PHP Fatal error:  Allowed memory size of 262144 bytes exhausted (tried to allocate xxxx bytes) in /The/Full/file/path/file_name.php on line xx

So, how to fix it? Easy, just increase the allowed memory size. But where to change? There are several points. Let’s say you want to set it to 512MB. Then –

  1. If you want to change it for that script only, modify ini setting at the beginning of file.
    ini_set('memory_limit', '512M');
  2. If you need to change for entire project, add this line in .htaccess of projects root directory (apache only)
    php_value memory_limit 512M
  3. For making the change systemwide, in php.ini, find and set
    memory_limit = 512M

Troubleshooting

Okay. Now, if you see the error is still showing, check the following things –

  • Most common mistake- if you’ve changed php.ini, don’t forget to restart web server
  • Check phpinfo(), if the value you’ve set is not there, maybe your’e changing in wrong php.ini file
  • Check the line you modified in ini file is not commented. (there is no ; at the beginning of line)
  • The value will be 512M not 512MB
  • Check if your script require more memory even after doing the change. (You may set the value to ‘-1’ to make it unlimited. But, think seriously before doing it!)

These are all the mistakes regarding this issue I did and saw people doing. If you’ve experienced something else, please let me know. Thank you.

Leave a Comment

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