Skip to content
Start » wordpress » Increasing the Memory Limit in PHP for WordPress

Increasing the Memory Limit in PHP for WordPress

As WordPress is a CMS (Content Management System) with the possibility of using external themes and plugins, it is common for some errors to occur after installing, even for testing, a new extension of this nature on your site.

It is also susceptible to some errors when a PHP or Apache or Nginx version upgrade occurs, due to the complexity of keeping all these plugins and themes up to date and compatible with the most current versions of these software.

The ideal is to check in the official documentation of the extensions if they already support a new version of WordPress that you intend to update, either the PHP language or the Apache HTTP server or Nginx.

However, even after you analyze, study and decide for upgrades, errors can happen in your installation.

Some will give you an indication on the website pages, others will allow you to check in the WordPress administrative environment but there are errors that will completely break access to the website, returning the infamous 500 ERROR.

Memory limit in PHP

One of the most common problems of this nature is related to the exhaustive consumption of memory, which often happens when installing or updating a buggy theme or plugin.

In fact, memory limit errors can affect other systems developed in PHP, as in Laravel framework, for example.

The general message is as follows:

Fatal error: Allowed memory size of 41943040 bytes exhausted (tried to allocate 20480 bytes).

The values may appear different for your server.

The free translation would be:

Fatal error: Allowed memory size of 41943040 bytes has been exhausted (attempted to allocate 20480 bytes).

Another way the message with this error can appear is:

PHP Fatal error: Out of memory (allocated 4193040 bytes) (tried to allocate 20480 bytes).

The free translation would be:

PHP Fatal Error: Out of memory (allocated 4193040 bytes) (attempted to allocate 20480 bytes).

These error messages mean that WordPress needed more memory to run a script in one click. Http Request, greater than what is allowed in your PHP installation configuration.

In this example PHP has already allocated 4193040 bytes (around 40M) and the attempt to allocate the next 20480 bytes was not possible because the limit was exceeded.

This allowed memory usage ratio is configured in the directive memory_limit of the file php.ini of your installation.

memory_limit:

Defines the maximum amount of memory in bytes that a script can allocate. This helps to prevent poorly written scripts from consuming all available memory on the server. Note that for there to be no memory limit, this directive must be set to -1. When an integer is used, the value is measured in bytes. The abbreviated notation, as described in this FAQ, can also be used.

PHP manual

Increasing the memory limit in PHP

There are some ways to increase the memory limit for executing PHP scripts, below I start showing the global configuration format on the server, in general, the most indicated.

Next, how to configure it in the root of any PHP project, and finally, how to increase it within a specific script.

Setting memory limit in php.ini file

In general a change to the file php.ini, following the example below, can solve. Look in the section resources limits, change the configuration variable memory_limit.

In general I start conservatively and go up from the traditional 40M to 64M, but nothing prevents you from going up to 128M as an example to follow.

Remember, the following snippet is a sample of the lines where you should find the configuration, in the file php.ini, of maximum limit for PHP on this server.

; How many GET/POST/COOKIE input variables may be accepted ; max_input_vars = 1000 ; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit = 128M

Setting PHP memory limit in .htaccess configuration

It is possible to set the PHP execution memory limit setting directly in the file .htaccess of a project. When to do this:

  • in some situations why you don't want the configuration of a particular project to impact the global configuration of the server
  • why you are not allowed to change the file php.ini

To do this, open the file .htaccess that you use for your PHP project and add the following line:

php_value memory_limit 128M

Note that depending on your server configuration, the possibility of overriding settings using the .htaccess may be disabled.

Increasing memory limit in WordPress configuration

In case you are working with a shared server host and you are not allowed to change PHP settings.

Or, in cases when you have more than one software installed on the same server and you don't want to generally increase this limit to maintain a more granular level of control.

You can opt for the strategy of changing the memory threshold value on WordPress execution by changing the file wp-config.php.

To do this, just add the following line to the beginning of the file:

<?php

ini_set('memory_limit','128M');

Final considerations

It is necessary to maintain constant organization and maintenance in your WordPress installation so that your website, in addition to being safe and stable, remains in constant operation, avoiding loss of access and audience for your company or content.

keep up to date

Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and courses free software in computer programming, quality software development, software engineering and tools like WordPress, Git, Gitlab, Github, among others.

More about WordPress and PHP

Best Practices for Writing Clean Code
Neste artigo apresento dicas para ao desenvolver um software, escrever com boas práticas de código limpo, o que, em geral, significa escrever blocos de código mais legíveis, em alguns contextos, mais fluídos e, bem estruturados. …
Laravel PHP – Class 'NumberFormatter' Not Found
Ao instalar uma aplicação em um servidor Windows (necessidade de projeto, adequação a infraestrutura do cliente), me deparei com o erro "NumberFormatter Not Found". A primeira reação foi verificar se havia esquecido algum commit de …
How to resolve “COMPOSER_AUTH” does not match the expected JSON schema in Laradock
When using Laradock, accessing the Workspace container, I have sometimes received the second error when trying to run php composer: "COMPOSER_AUTH" does not match the expected JSON schema, this may result in errors and should …
Free e-books on programming and computing in general
Looking for free e-books on programming, software development, and other areas of computing? This list includes these topics as well as books on software engineering and other subjects. The pandemic period has exploded…
How to repopulate with old the input file upload field, in PHP and Laravel
After validating data entry forms, which return to the user with information about the errors that must be corrected, the good practice of populating the fields with the old values (old) is not …
Reasons to choose and use PHP as a programming language
Many beginners and even experienced developers, but who do not know the language, question what are the reasons to choose, and definitely use PHP as the programming language for a small or …

Leave a Reply

Your email address will not be published.

en_USEnglish
%d bloggers like this: