Important: Shared hosting accounts are limited to no more than 64M, even if the limit is set to be higher inside of the php.ini file. VPS or Dedicated hosting services can set the limit to match their RAM. To help your website run at maximum performance, it is optimal for the memory limit to be set as low as possible while still allowing your website to function.
PHP has a memory limit configuration that determines the maximum amount of memory a single PHP script can consume.
This memory_limit is usually defined in a php.ini file as follows:
memory_limit = 128M
The default memory_limit configuration for PHP is 128M or 128 Megabytes. You can replace this value in your php.ini file with any other ; π limit to 256 Megabytes memory_limit = 256000000 ; π this is the same memory_limit = 256M 1 value.
Without the ; π limit to 256 Megabytes memory_limit = 256000000 ; π this is the same memory_limit = 256M 2 symbol, the limit will be measured in bytes:
; π limit to 256 Megabytes memory_limit = 256000000 ; π this is the same memory_limit = 256M
The memory limit in PHP is calculated on a pre-script basis, meaning that each PHP script request is independent of the other.
For example, if you have 3 running PHP scripts where each consumes 90MB of memory, then the total memory used by PHP will be 270MB passing the 128MB limit.
When you have a single PHP script that consumes 200MB of memory, then PHP will throw an ; π limit to 256 Megabytes memory_limit = 256000000 ; π this is the same memory_limit = 256M 3 fatal error.
To remove the memory_limit, you can set the value as ; π limit to 256 Megabytes memory_limit = 256000000 ; π this is the same memory_limit = 256M 5 like this:
memory_limit = -1
This way, PHP wonβt have memory limitations and can consume all available memory on your server.
Aside from editing the php.ini file, you can also edit the ; π limit to 256 Megabytes memory_limit = 256000000 ; π this is the same memory_limit = 256M 7 file as shown below:
php_value memory_limit 256M
You can get the current memory_limit value with the ; π limit to 256 Megabytes memory_limit = 256000000 ; π this is the same memory_limit = 256M 9 function:
echo ini_get("memory_limit"); //128M
You can also change the memory limit configuration only for a specific script using memory_limit = -1 0 function:
// π set memory limit as 256M using ini_set() echo ini_set("memory_limit", "256M");
And thatβs how you set the memory_limit configuration in PHP.
If youβre using editing the memory_limit for a WordPress site, keep in mind that WordPress has its own memory constants defined as:
- memory_limit = -1 3 sets the memory limit in WP frontend area
- memory_limit = -1 4 sets the memory limit in WP administration area
The memory_limit = -1 3 is usually set at 64MB for WP multisite and 40MB for a single site, while memory_limit = -1 4 is usually set at 256MB.
You can define these constants in memory_limit = -1 7 file, below the memory_limit = -1 8 definition as shown below:
define( 'WP_DEBUG', true ); /* Add any custom values between this line and the "stop editing" line. */ define( 'WP_MEMORY_LIMIT', '300M' ); define( 'WP_MAX_MEMORY_LIMIT', '300M' );
WordPress configuration doesnβt override the PHP configuration, so make sure you have memory_limit at 300MB or more when you set memory_limit = -1 3 and memory_limit = -1 4 at 300MB.