Need help? Chat now!

Hostwinds Tutorials

Search results for:


Table of Contents


Common Causes of PHP Memory Errors
How to Fix PHP Memory Errors
Increase My PHP Memory

PHP Memory Errors

Tags: WHM 

Common Causes of PHP Memory Errors
How to Fix PHP Memory Errors
Increase My PHP Memory

Note: This article is for Hostwinds clients who have access to WHM on their VPS, Cloud, or Dedicated Server.

Common Causes of PHP Memory Errors

Memory size errors commonly occur on a site built with open source CMS systems like WordPress, Joomla, Drupal, and Moodle. While there may be other factors, the primary cause is that the PHP's default memory limit setting isn't allowing enough memory to run or load the request.

How to Fix PHP Memory Errors

The most efficient way to fix this is to increase the memory limit that PHP allows to process. However, if you cannot change the memory on your plan, you may fix the error using the steps provided below.

A note for shared hosting services and Hostwinds clients using WordPress: The wp-config file may be limiting the amount of memory available, as it may be allocating less than what PHP is allocating. You can increase the amount of memory WordPress allows in this file, but the amount in the php.ini file will determine the maximum amount allowed.

If you are comfortable with PHP, it is possible to increase the memory limit programmatically in your code. You will give PHP extra memory if a piece of code gets called instead of a sitewide increase for every PHP process.

\<?php

ini_set('memory_limit', '1024M'); // or you could use 1G

If the error is fixed by increasing the memory limit and your code now works, you need to continue onwards and decrease the memory usage of your site further. Below are recommended steps that you can take. Note that while you don't have to do all of them, it will always help you to future-proof your site:

  1. Disable PHP extensions that you are not using and do not need.
  2. Upgrade your PHP to the newest version available. PHP is consistently updating, and each version is known to use much less memory than its previous releases.
  3. When reading files, read them line-by-line instead of reading the complete file into memory. You can use fgets and SplFileObject::fgets to do so, and PHP.net has robust documentation to help you along the way.
  4. Stop loading big datasets into an array and try to process smaller subsets of the dataset. If you need to, you can persist the data into a database.
  5. Check the versions of any third-party libraries you are using, as updates of these libraries usually take less memory use.
  6. Upgrade any PHP extensions with possible memory leaks or delete them if you are not using them.
  7. If line-by-line reading is impossible due to file size, you can break the file into many smaller files and process those individually.
  8. For the amount of code causing issues, unset variables containing large amounts of data that aren't required later in the code.

In addition, make sure PHP applications have enough memory allocated to them. If it is too low, it will cause a fatal error.

Increase My PHP Memory

Alternatively, here are the steps you can take to increase the memory limit. Please note that if you are on a Shared Hosting Plan, you will need to upgrade your service to do so.

Step 1: Log in to WHM for the server you are going to upgrade. Note that you can find details to log into your WHM in the welcome email received upon purchase.

Step 2: Go to the PHP configuration editor.

Step 3: Edit the "memory_limit" variable to the size that you wish.

Step 4: Save your changes.

While this may help for a while, the errors are likely to return. If you're getting this error, there's a high probability that your code needs to be trimmed and optimized.

Note for shared hosting services and Hostwinds clients using WordPress: The wp-config file may be limiting the amount of memory available, as it may be delivering less than what PHP is allocating. You can increase the amount of memory WordPress allows in this file, but the amount in the php.ini file will determine the maximum amount allowed.

Example: If PHP only allows 128MB and you change the wp-config file to allow 512MB, you will only get 128MB. But PHP might be allowing more than the wp-config file, so increasing the amount in the wp-config could help if that is the case. If you have upgraded your account to a VPS or Dedicated Server, you can control this setting fully.

Written by Hostwinds Team  /  December 13, 2016