How to Fix PHP Error Out of Memory


Symptoms

Either a blank page during website load or a fatal error as shown in the example below:

Out of memory (allocated 257425408) (tried to allocate 33554402 bytes)
Allowed memory size of 67108864 bytes exhausted (tried to allocate 281639 bytes)

Cause

The cause of this fatal error is memory limitation. Every web server allocates a certain amount of memory for PHP execution. This amount is typically defined in the php.ini configuration file. If PHP tries to allocate more memory than allowed the fatal error is raised and the process is halted.

There are few possible reasons why an application or website would trigger this fatal error:

  • PHP script manipulating a large amount of data;
  • PHP script allocating a lot of memory for objects, arrays, resources etc.

Requirements

Before you start, please make sure that you have the ability to perform the following actions on your web server. Otherwise consider contacting your website hosting provider for assistance.

  • Modify php.ini file (PHP configuration file)
  • Restart Apache web server (to apply php.ini changes)

Read Me First

Based on our experience with web applications that cause this fatal error, increasing the memory limit solves the issue for only a short period of time. Eventually the application starts to work with larger amounts of data, leading to more memory allocation and causing the same error occurs again.

The only permanent solution to this error is code optimizing. Ideally, any public facing web application should not allocate more than 2Mb of memory.

Please note. Most shared hosting providers do not allow allocation of more memory than previously allowed. The solution below will not work in those situations. You would need to consider to optimizing your web application instead.

Resolution

Open your web server’s php.ini file and locate the parameter memory_limit. Try doubling the size of the currently value.

memory_limit = 512M

For more information about the memory_limit property, check the official PHP documentation.

Fix WordPress PHP errors instantly with CodePinch!

Get Started
Thank You! Message Submitted Successfully!