Memory Limit PHP: Complete Guide to Fix Memory Errors 2025

Learn how to fix PHP memory limit errors with our step-by-step guide. Increase memory limits, optimize performance, and troubleshoot common issues easily.

Suresh Ramani - Author - Techvblogs Suresh Ramani
2 days ago
Memory Limit PHP: Complete Guide to Fix Memory Errors 2025 - Techvblogs

Are you getting “Fatal error: Allowed memory size exhausted” in your PHP website? Don’t worry - you’re not alone. This comprehensive guide will help you understand PHP memory limits and fix memory issues once and for all.

What is PHP Memory Limit?

PHP memory limit is like a safety cap on how much computer memory your PHP script can use. Think of it as a water bucket - once it’s full, it can’t hold any more water. Similarly, when your PHP script reaches its memory limit, it stops working and shows an error.

Why does this matter? Without memory limits, a single webpage could crash your entire server by using all available memory. This protects your website and keeps it running smoothly.

How PHP Memory Limit Works (Simple Explanation)

When someone visits your website:

  1. PHP starts with zero memory usage
  2. As the page loads, it uses more memory for images, data, and code
  3. If it tries to use more memory than allowed, PHP stops and shows an error
  4. The page fails to load properly

Real-world example: If your memory limit is 128MB and your script tries to use 129MB, it will crash with a memory error.

Common Memory Limit Values by Hosting Type

Different hosting providers set different memory limits:

  • Shared Hosting: 64MB - 128MB (most common)
  • WordPress Hosting: 128MB - 256MB
  • VPS/Cloud Hosting: 256MB - 512MB
  • Dedicated Servers: 512MB - 1GB+

Note: These are typical ranges. Your actual limit may vary.

When You Need to Increase Memory Limit

You might need more memory if you’re:

  • Running WordPress with many plugins
  • Processing images (especially large photos)
  • Importing data from CSV files
  • Using page builders like Elementor or Divi
  • Running e-commerce sites with many products

How to Check Your Current Memory Limit

Method 1: Create a PHP Info File

  1. Create a new file called info.php
  2. Add this code:
<?php
phpinfo();
?>
  1. Upload to your website root folder
  2. Visit yourwebsite.com/info.php
  3. Look for “memory_limit” in the results

Method 2: WordPress Users

Install the “Health Check & Troubleshooting” plugin to see your memory limit in the WordPress admin area.

Method 3: Check via Code

Add this to any PHP file:

<?php
echo "Current memory limit: " . ini_get('memory_limit');
?>

How to Increase PHP Memory Limit (5 Methods)

Method 1: Edit php.ini File (Most Effective)

  1. Find your php.ini file (usually in your hosting control panel)
  2. Look for the line: memory_limit = 128M
  3. Change it to: memory_limit = 256M
  4. Save the file and restart your server

Method 2: WordPress wp-config.php

  1. Access your WordPress files via FTP or file manager
  2. Open wp-config.php
  3. Add this line before “/* That’s all, stop editing! */”:
define('WP_MEMORY_LIMIT', '256M');
  1. Save the file

Method 3: .htaccess File

  1. Open your .htaccess file in the root directory
  2. Add this line:
php_value memory_limit 256M
  1. Save the file

Method 4: Contact Your Hosting Provider

If the above methods don’t work, contact your hosting support. They can increase the limit from their end.

Method 5: Runtime Configuration (Temporary)

Add this to your PHP script:

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

Best Practices for Memory Management

1. Monitor Your Memory Usage

Use this code to check how much memory your script uses:

echo "Memory used: " . memory_get_usage(true) / 1024 / 1024 . " MB";

2. Optimize Your Code

  • Remove unused plugins and themes
  • Optimize images before uploading
  • Use caching plugins
  • Clean up your database regularly

3. Choose the Right Memory Limit

  • Small websites: 128MB is usually enough
  • Medium websites: 256MB works well
  • Large websites: 512MB or more may be needed

4. Don’t Set It Too High

Setting memory limits too high can:

  • Make your website vulnerable to attacks
  • Slow down your server
  • Waste server resources

Troubleshooting Common Memory Issues

Problem: WordPress Admin is Slow

Solution: Increase memory limit to 256MB and deactivate unnecessary plugins.

Problem: Image Upload Fails

Solution: Increase memory limit and optimize images before uploading.

Problem: Plugin Installation Fails

Solution: Temporarily increase memory limit to 512MB for installation, then reduce it back.

Problem: Page Builder Crashes

Solution: Page builders like Elementor need at least 256MB to work properly.

Security Considerations

Important: Always set reasonable memory limits. Here’s why:

  • Too low: Your website won’t work properly
  • Too high: Hackers could crash your server
  • Just right: Your website works well and stays secure

Recommended approach: Start with 256MB and increase only if needed.

Expert Tips from a Senior Developer

After 8+ years of PHP development, here are my top recommendations:

  1. Always test changes on a staging site first
  2. Monitor memory usage regularly using tools like New Relic or GTmetrix
  3. Keep backups before making any changes
  4. Use professional hosting for better memory management
  5. Update PHP regularly - newer versions use memory more efficiently

Conclusion

Understanding PHP memory limits is crucial for running a successful website. Start with 256MB for most WordPress sites, monitor your usage, and adjust as needed. Remember to always test changes on a staging site first and keep backups.

If you’re still having issues after following this guide, consider contacting a professional developer or upgrading to better hosting.

Need help? Leave a comment below, and I’ll personally help you solve your memory limit issues.

Frequently Asked Questions

Setting memory limit too high can make your server vulnerable to attacks and waste resources. It won’t necessarily make your website faster.

Technically yes (by setting it to -1), but this is dangerous and not recommended for websites. Only use unlimited memory for command-line scripts.

This usually happens because: Your hosting provider overrides your settings You’re editing the wrong configuration file Server restarts reset temporary changes

A typical WordPress site needs: Basic site: 128MB Site with plugins: 256MB E-commerce site: 512MB or more

Not directly. However, if your site hits the memory limit, it will crash and appear slow or broken to visitors.

memory_limit: Controls how much memory a script can use max_execution_time: Controls how long a script can run

For a very basic HTML/PHP site, yes. But for WordPress or any modern CMS, 128MB is the minimum recommended.

Watch for these signs: “Fatal error: Allowed memory size exhausted” messages Pages loading slowly or not at all Admin area becoming unresponsive Plugin installation failures

Yes, you can set memory limits in individual PHP files using ini_set(), but this is not recommended for beginners.

Comments (0)

Comment


Note: All Input Fields are required.