Manage cookies that are used for advertising, such as ad personalization, remarketing, and ad effectiveness analysis.
2.4.3.8. Error Fatal error: Allowed memory size
The error "Fatal error: Allowed memory size" means that the script has exceeded the value of the PHP memory_limit parameter of the current hosting plan during its execution.
Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes) in /home/example/example.com/www/path/to/script.php on line Z
X— the available memory volume specified by the PHP parameter memory_limit, in bytes.Y— the amount of memory the script attempted to use beyond the available volume at the time the error occurred, in bytes./home/example/example.com/www/path/to/script.php— the full path to the script during the execution of which the error occurred.Z— the line number in the script where the error occurred.
Attention!
The sum of the valuesX + Y is not the required value for memory_limit, because Y indicates the amount of memory that was insufficient at the time of the script's crash, and if the script continues to run, it may require a significantly larger amount of memory.
Possible reasons
The error most often occurs in the following situations:
- The most common situation where a script exhausts all available memory is when working with images. PNG and JPG formats compress the image, but PHP loads it into memory pixel by pixel, as if it were saved in BMP format. This results in the script running out of memory to process the image. To prevent this error, it is advisable to reduce the size of images before uploading them to the site and not rely on PHP to change the format of high-resolution images on its own.
- Executing database queries that return excessively large volumes of information, which do not fit into the available memory.
- Processing large XML files.
- Running resource-intensive or simply unoptimized scripts and modules.
Solution options
The solution to the problem may be:
- Changing the memory_limit value in PHP settings, if the maximum available value for the current plan is not set.
- If the site uses scripts from outdated versions, be sure to check whether the PHP version installed for the site matches the version recommended by the site developer. If not, change it to the required version and check the site's functionality after approximately 10-15 minutes.
- Reducing the size of files processed by the script, analyzing the operation and optimizing the script or module itself to decrease its memory consumption.
- Changing the plan to another one with a higher memory_limit.
Attention!
When changing the tariff plan, it should be noted that this will only help in cases where memory requirements are set by CMS developers, as well as in some cases of processing large files. In the case of a resource-intensive script (usually custom-written), the script itself needs to be corrected.
Note for WordPress: If the actual allocated memory size (value X) is less than the PHP memory_limit value used by the hosting plan, it is necessary to add the following line to the wp-config.php file:
define('WP_MEMORY_LIMIT', 'xxxM');
Where xxx is the value of the memory_limit of the plan.