# Fix WordPress Memory Limit Exceeded
The dreaded white screen with an error message like "Fatal error: Allowed memory size of 67108864 bytes exhausted" means WordPress ran out of PHP memory. This isn't about disk space; it's about RAM allocated to PHP scripts.
Every plugin, theme, and WordPress itself consumes memory when processing requests. Complex operations like image resizing, large imports, or memory-hungry plugins can push you over the limit.
Recognize the Error
The error typically appears in one of these forms:
``` Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/wp-includes/plugin.php on line 256
Fatal error: Out of memory (allocated 78643200) (tried to allocate 20240 bytes) in /var/www/html/wp-content/plugins/some-plugin/plugin-file.php on line 123
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted ```
You might also see:
- White screen of death (if error display is off)
- "There has been a critical error on this website"
- WordPress admin showing "Briefly unavailable for scheduled maintenance"
Check Your Current Memory Limit
Before increasing the limit, know what you're working with:
Via WP-CLI
```bash # Check WordPress memory limit wp eval 'echo "WP Memory Limit: " . WP_MEMORY_LIMIT . "\n";'
# Check PHP memory limit wp eval 'echo "PHP Memory Limit: " . ini_get("memory_limit") . "\n";'
# Check memory usage wp eval 'echo "Memory Usage: " . round(memory_get_usage(true) / 1024 / 1024, 2) . " MB\n";' ```
Via WordPress Admin
- 1.Go to Tools > Site Health > Info
- 2.Scroll to "Server"
- 3.Find "PHP memory limit" and "WordPress memory limit"
Via PHP Info
Create a temporary file phpinfo.php in your WordPress root:
<?php phpinfo(); ?>Visit yourdomain.com/phpinfo.php and search for "memory_limit". Delete the file immediately after checking.
Increase the Memory Limit
Method 1: wp-config.php (Recommended)
Edit wp-config.php and add this line before /* That's all, stop editing! */:
define('WP_MEMORY_LIMIT', '256M');For admin operations that need more memory:
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');WP_MEMORY_LIMIT applies to the frontend. WP_MAX_MEMORY_LIMIT applies to the admin area.
Method 2: .htaccess (Apache)
Add to your .htaccess file:
php_value memory_limit 256MThis may not work on all hosts; some disable PHP overrides via .htaccess.
Method 3: php.ini
If you have access to php.ini:
memory_limit = 256MOn shared hosting, you might need to create a .user.ini file:
memory_limit = 256MMethod 4: wp-config.php for Specific Operations
For one-off memory-intensive operations, temporarily increase the limit:
// At the top of wp-config.php
ini_set('memory_limit', '512M');Or within a specific script:
// In your functions.php or custom script
add_action('init', function() {
if (is_admin()) {
ini_set('memory_limit', '512M');
}
});Verify the Change
```bash # Check the new limit wp eval 'echo "New limit: " . ini_get("memory_limit") . "\n";'
# Test a memory-intensive operation wp eval 'for ($i = 0; $i < 100000; $i++) { $array[] = str_repeat("x", 1000); } echo "Memory test passed\n";' ```
When Increasing Memory Doesn't Work
If you've increased memory but still get errors, the problem might be elsewhere.
Hosting Provider Limits
Many shared hosts enforce a hard memory ceiling. Even if you set 512M in wp-config, the host might cap you at 128M.
Check actual available memory:
wp eval '$limit = ini_get("memory_limit"); $bytes = return_bytes($limit); echo "Actual limit: $limit (" . number_format($bytes) . " bytes)\n"; function return_bytes($val) { $val = trim($val); $last = strtolower($val[strlen($val)-1]); $val = (int)$val; switch($last) { case "g": $val *= 1024; case "m": $val *= 1024; case "k": $val *= 1024; } return $val; }'If the limit doesn't change, contact your host or upgrade to a VPS.
Memory Leak in Plugin or Theme
A poorly coded plugin might have a memory leak.
Identify memory-hungry plugins:
# Get memory usage per plugin
wp eval '
$plugins = get_option("active_plugins");
foreach ($plugins as $plugin) {
$before = memory_get_usage(true);
include_once WP_PLUGIN_DIR . "/" . $plugin;
$after = memory_get_usage(true);
$diff = round(($after - $before) / 1024 / 1024, 2);
echo "$plugin: ${diff}MB\n";
}
'Infinite Loop or Recursion
The error might point to a genuine bug, not just insufficient memory:
# Check debug log for clues
grep -i "memory\|fatal" wp-content/debug.log | tail -20If you see the same function repeated in stack traces, that's likely an infinite loop.
Optimize Memory Usage
If you can't increase memory further, reduce consumption.
Disable Unnecessary Plugins
```bash # List active plugins wp plugin list --status=active --fields=name,title
# Deactivate non-essential plugins wp plugin deactivate plugin-name --exclude=essential-plugin ```
Use Lightweight Alternatives
Replace memory-heavy plugins:
- Contact forms: Use Contact Form 7 instead of heavy form builders
- SEO: Use lightweight schema plugins instead of all-in-one SEO
- Page builders: Generate static HTML instead of dynamic rendering
Increase Object Caching
Object caching reduces memory pressure by caching database queries:
// In wp-config.php
define('WP_CACHE', true);Or use Redis:
wp config set WP_REDIS_HOST redis
wp config set WP_REDIS_PORT 6379Limit Revisions and Autosaves
// In wp-config.php
define('WP_POST_REVISIONS', 3);
define('AUTOSAVE_INTERVAL', 300); // 5 minutesClean Up Database
Large databases consume more memory:
```bash # Remove post revisions wp post delete $(wp post list --post_type=revision --format=ids) --force
# Remove spam comments wp comment delete $(wp comment list --status=spam --format=ids) --force
# Remove transients wp transient delete --all
# Optimize tables wp db optimize ```
Quick Reference Table
| Memory Setting | Applies To | Typical Value |
|---|---|---|
WP_MEMORY_LIMIT | Frontend operations | 128M-256M |
WP_MAX_MEMORY_LIMIT | Admin operations | 256M-512M |
php.ini memory_limit | All PHP processes | 256M-512M |
.htaccess php_value | Apache PHP processes | 256M |
Summary Checklist
- 1.Check current memory limit via WP-CLI or Site Health
- 2.Add
define('WP_MEMORY_LIMIT', '256M');to wp-config.php - 3.Verify the change took effect
- 4.If still failing, check for plugin conflicts
- 5.If on shared hosting, confirm host allows memory increases
- 6.Consider optimizing memory usage as a long-term solution
Memory errors are straightforward once you know where to look. Increase the limit where PHP can see it, and you're usually back in business within minutes.