How to Fix Out of Memory Script Errors

A common problem you might face when running PHP scripts is an “out of memory” error. It usually looks something like this:

PHP Fatal error: Out of memory (allocated 51795435) (tried to allocate 84524 bytes) in /home/user/public_html/randomdirectory/file.php on line 750

First, it is important to understand what this error means. PHP is configured to only allow a certain amount of memory to be allocated to a script. This is a security and performance precaution so that a single script does not exhaust all of your RAM. In most cases, the PHP memory limit is set much lower than it needs to be by default.

To change the global PHP memory limit, edit your php.ini file. In CentOS, it is usually located at: /etc/php.ini.

You should see a line in the file that looks like:

memory_limit = 16M

You can raise that amount to whatever you need to make your script function properly while also not taking up too much memory so as to compromise the integrity of your system. For example, you can set it to:

memory_limit = 64M

This should fix your problem. If your script continues to need more memory no matter how much you allocated to it, the script itself may be the problem, and you should investigate further.

 











Comments: