We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Improve your Craft CMS skills

How Do I Up My Memory Limit In Craft CMS?

10 min read
Shape April 2022 HR 45

Hitting PHP memory limits causes big headaches for Craft CMS sites. With cache bloat, complex templates, and abundant plugins, the default 128MB ceiling cramps performance. Boosting PHP memory limit solves 500 errors, improves stability, and unlocks faster page loads. This guide provides expert techniques to accurately determine Craft's current memory allocation and increase it for a smooth, optimized experience.

The PHP memory limit is set in php.ini, usually 128M by default. For optimal Craft performance, increase this to 512M, 768M, or higher as needed. Use .htaccess temporarily or edit php.ini permanently. Monitor Craft's memory usage, optimizing caches and plugins first. Adjust the limit incrementally until errors stop and site performance improves.

Increasing PHP Memory Limit for Craft CMS

Overview of PHP Memory Limits

PHP memory limits control the maximum amount of memory a PHP script can use. The default memory limit is typically 128MB. This is set in the php.ini configuration file.

When a PHP script requires more memory than the limit allows, it will reach the ceiling and crash. For complex web applications like Craft CMS, the default PHP memory limit can be restrictive. Craft utilizes memory for various operations like processing requests, caching data, and rendering front-end templates.

With larger sites containing lots of entries, assets, and complex Twig templates, Craft CMS can bump up against the default PHP memory ceiling. This will cause website errors and affect site performance. Increasing the PHP memory limit provides Craft CMS with more headroom to operate smoothly.

How Craft CMS Uses Memory

Craft CMS leverages memory in a variety of ways that can be impacted by restrictive PHP memory limits.

Caching is one of the biggest memory consumers. Craft CMS caches elements, templates, and other data to optimize performance. This allows commonly used data to be stored in memory for faster access. The cache size grows as more entries, assets, and other content are added to the site. Exceeding the PHP memory limit will disrupt Craft's caching and degrade performance.

Twig template rendering also requires significant memory overhead. Complex Twig templates with nested loops, conditionals, and many variables take more memory to render. The front-end output is processed entirely in memory before displaying the rendered HTML. Sites with hundreds of templates can benefit from additional memory capacity.

Memory is also needed for general request processing, image manipulation, plugin and module operations, and other procedural PHP operations. All of these cumulative memory needs can push Craft CMS sites over the default 128MB PHP ceiling.

Benefits of Increasing PHP Memory Limit

Increasing the PHP memory limit provides several advantages for Craft CMS installations:

  • Faster Page Loads - More memory allows Craft to leverage caching for quicker page loads. With sufficient room to cache templates, elements, etc. pages can be assembled faster.

  • Reduced Server Load - Increased caching means fewer queries and processing required per request. The server doesn't work as hard with more data cached in memory.

  • Smoother Asset Processing - Manipulating images and assets requires significant memory overhead. A higher limit prevents crashes and errors when processing large files.

  • More Stability - Out of memory errors that crash PHP are eliminated. The site remains stable and reliable with adequate memory headroom.

  • Support Large Sites - Large sites with abundant content, traffic, and complex templates can operate smoothly with a higher memory ceiling. Removes memory bottlenecks.

  • Run More Plugins - Running additional plugins that consume memory won't overload Craft as easily. More capacity allows more add-ons. Keep plugins updated.

  • No PHP Segmentation - Segmenting PHP across multiple smaller processes degrades Craft's performance. More memory avoids segmentation.

Increasing PHP memory provides tangible benefits for Craft CMS performance and stability. Sites running at scale with abundant traffic, assets, and integration needs will see the most dramatic gains from a higher memory limit. It's an easy upgrade that unlocks better Craft CMS performance.

Determining the Current PHP Memory Limit for Craft CMS

Finding the php.ini Memory Limit

The primary PHP memory limit is set in the php.ini configuration file. This is usually located in the default PHP folder, but the path can vary based on server setup and OS.

On Linux, php.ini may reside at /etc/php/7.4/cli/php.ini or /etc/php/7.4/apache2/php.ini by default. For MacOS, check usr/local/etc/php/7.4/php.ini. On Windows, look in C:\php\php.ini.

Use phpinfo(); in a PHP script to see the actual loaded php.ini path for your server.

Once the php.ini file is located, open it in a text editor. Search for the "memory_limit" setting, usually found in the section marked ; Resource Limits. This will show the current PHP memory limit in megabytes (MB). The common default is 128M or 128MB.

Knowing the exact memory limit set in php.ini is the first step to determining the allocation for a Craft CMS site. However, other overrides may increase or decrease this base value.

Checking for .htaccess Overrides

The .htaccess file used by Apache web servers can override php.ini directives, including the memory limit. Any memory allocation set in .htaccess takes precedence.

To check for a .htaccess override on a Craft CMS installation, open the .htaccess file in the web root. Look for a line such as:

Copy code:

php_value memory_limit 256M

This would increase the memory limit to 256MB. Commenting out such a line will eliminate the .htaccess override and fall back to php.ini.

Keep in mind multiple .htaccess files can exist if using a subfolder for Craft CMS - check both

the root and /craft folders. The override closest to the executing script takes priority.

Removing any .htaccess memory limit directives ensures php.ini dictates the true memory ceiling. This avoids conflicting settings across configuration files.

Other Environment Memory Limit Settings

Beyond php.ini and .htaccess, the memory limit could also be set via:

  • PHP environment variables in vhost/server configs

  • The PHP config file in cpanel/WHM

  • Per-directory .user.ini files

  • The Craft CMS config/general.php file

Check PHP environment variables for a line like:


Copy code:


See if the cpanel PHP config or any .user.ini files include memory directives.

Also look in craft/config/general.php for a custom 'memoryLimit' => '256M' line limiting Craft's memory usage specifically.

These less common locations can override the standard php.ini and .htaccess locations. Tracking down and eliminating custom memory settings avoids confusion down the road.

The best practice is to have just one definitive memory limit source, usually php.ini for system-wide control. This centralizes management for Craft CMS deployments.

However, in certain cases like shared hosts, .htaccess overrides may be the only way to tweak memory higher than the system php.ini allows. When using overrides, diligently document and comment them to prevent future uncertainty.

Knowing the current memory limit in play for a Craft CMS site ensures there are no surprises. And it prepares you to adjust the memory higher if needed, in an organized fashion from a single well-managed source.

Increasing the PHP Memory Limit for Craft CMS

Editing php.ini for Permanent Increases

The best practice for permanently increasing PHP memory limit is to edit the php.ini file. This raises the ceiling system-wide for all PHP applications.

Locate your php.ini file as covered in the previous section. Open it in an editor like Notepad++ or VI that can handle the formatting.

Look for the "memory_limit" line under the Resource Limits section. It will likely be set to 128M or 256M by default.

Increase this value higher as needed for Craft CMS - common values are 512M, 768M, 1024M or higher. Save php.ini after making this change.

Keep in mind the memory limit is per-process. Multiply the php.ini setting by your max PHP processes to calculate total memory usage.

Changes to php.ini require restarting PHP. On Linux, restart Apache and PHP with sudo service apache2 restart. On Windows, restart IIS or Apache.

Test the new memory limit by checking phpinfo() to validate php.ini was updated. Monitor

Craft's memory usage and increase in incremental steps as necessary.

If the system-wide memory limit cannot be raised due to shared hosting constraints, .htaccess overrides provide more flexibility.

Using .htaccess for Temporary Changes

To temporarily increase the PHP memory limit for a Craft CMS site without altering php.ini, use .htaccess:

Copy code:

php_value memory_limit 256M

This .htaccess method sets the limit higher only for that particular site/folder. It does not affect wider PHP memory usage.

Place the .htaccess override in the Craft web root and/or the /craft folder. Test with phpinfo() to validate the change.

However, hard-coding the memory limit makes dynamic tuning difficult. Instead, define a variable:

Copy code:

php_value memory_limit ${CRAFT_MEMORY_LIMIT}M

Then set the variable's value in a separate .env file:

Copy code:


This technique allows easy changes without editing .htaccess directly. Just tweak the .env value.

.htaccess memory overrides should be temporary, with the php.ini limit increased system-wide for permanent changes. Avoid managing memory in multiple places.

Configuring Memory Limit in Craft CMS

For a Craft CMS specific memory limit, add a 'memoryLimit' => '256M' line to craft/config/general.php:

Copy code:

'memoryLimit' => '256M',

This limits memory for Craft operations like caching without changing system-wide PHP memory.

However, Craft cannot use more than the PHP memory ceiling set in php.ini/htaccess. The Craft config can only reduce memory, not increase it past PHP's limit.

So the Craft CMS memory setting should be slightly below the PHP memory ceiling to allocate headroom.

For example, with php.ini allowing 1024M total PHP memory, set Craft's memoryLimit to 768M.

This reserves memory for other PHP processes.

In summary, use Craft's memoryLimit to fine-tune memory for Craft's specific needs, while php.ini manages the PHP system memory limit.

Optimizing and Debugging Memory Limit Issues

Optimizing Craft to Reduce Memory Needs

Before increasing PHP memory limits, optimize Craft CMS to reduce its memory footprint:

  • Enable caching - Caching improves performance and reduces memory needs for subsequent requests. Enable Craft's template caching.

  • Simplify Twig templates - Complex Twig with lots of iterations and variables consumes more memory. Simplify tags where possible.

  • Reduce plugins/modules - Each plugin increases memory usage. Disable unneeded plugins. Avoid excessive modules.

  • Optimize queries - Inefficient queries result in larger dataset caching. Optimise SQL with eager loading and filtering.

  • Use thumbnail transforms - Smaller image thumbnails require less memory than originals. Create scaled asset transforms.

  • Increase PHP processes - More PHP processes allow memory to span across threads. Adjust based on traffic.

  • Delete stale data - Clear out old caches, logs, sessions etc. Backup and remove outdated entries.

  • Minify front-end code - Minification reduces code size, resulting in less data to cache/process.

Following performance best practices optimizes Craft CMS and lowers its memory demands before tweaking limits.

Debugging Common PHP Memory Errors

If Craft experiences 500 errors or installation failures, check for memory limit issues:

  • Review PHP and Craft logs for memory exhaustion fatal errors or exceptions.

  • Confirm the memory_limit in phpinfo() matches expectations.

  • Try a non-cached page load to test a fresh memory allocation.

  • Temporarily increase the memory limit as a test via .htaccess overrides.

  • Monitor memory usage with xdebug or webgrind to observe spikes.

  • Reduce the number of enabled plugins and modules to isolate memory hogs.

Addressing memory errors requires gathering data to pinpoint the exact failure causes. Then incrementally adjust the memory limit and optimize usage until stable.

Alternative PHP Servers for Better Memory Management

Standard PHP setups like PHP-FPM and mod_php have fixed memory limits per process/thread. This can lead to inefficient memory allocation across requests.

Alternative PHP servers like LiteSpeed and RoadRunner offer superior memory management:

  • LiteSpeed - Provides dynamic memory scaling per request to right-size usage. Prevents waste while still allowing bursts.

  • RoadRunner - Spawns isolated PHP processes instead of stateful threads for independent memory pools. More resilient to spikes.

  • Swoole - Optimized for long-running PHP with configurable memory recycling pools instead of stateful threads.

While standard PHP can run Craft CMS well, purpose-built PHP servers add additional memory optimization and scaling capabilities for large projects.

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat