When working with PHP-based applications, encountering a PHP Fatal Error: Out of Memory can be frustrating.
This error occurs when a script exceeds the allocated memory limit, preventing it from executing properly. If left unresolved, it can lead to website crashes, slow performance, or even complete inaccessibility.
Why Does This Error Happen?
There are several reasons why the PHP memory exhausted error occurs, including:
- The default PHP memory limit is too low for the script being executed.
- The application is processing a large amount of data or complex queries.
- Poorly optimized scripts that consume excessive memory.
- Shared hosting restrictions that limit memory allocation.
Impact of PHP Fatal Error: Out of Memory on Websites and Applications
If a PHP script runs out of memory, it can cause:
- Slow website performance – The server struggles to handle requests efficiently.
- Website crashes – The site may return a "500 Internal Server Error" or fail to load entirely.
- Interrupted processes – Background tasks like backups, image processing, or plugin updates may fail.
To ensure smooth operation, it’s crucial to increase PHP memory limit and optimize scripts to prevent this issue.
In the next sections, we’ll explore various solutions to fix PHP Fatal Error: Out of Memory and keep your application running without disruptions.
What Causes PHP Fatal Error: Out of Memory?
Understanding why the PHP Fatal Error: Out of Memory occurs is the first step to fixing it. This error happens when a PHP script exceeds the allocated memory limit set in the server configuration.
By default, PHP has a predefined memory limit, but if a script consumes more memory than allowed, it results in a PHP memory exhausted error, causing the script to terminate unexpectedly.
Common Causes of PHP Fatal Error: Out of Memory
Several factors can contribute to this error, including:
Low PHP Memory Limit
- The default PHP memory limit is often set too low (e.g., 128MB or 256MB), which may not be sufficient for complex scripts.
- Heavy applications like WordPress, Laravel, and Magento require more memory to function smoothly.
- Running scripts that handle large database queries, process big images, or generate extensive reports can cause PHP script runs out of memory errors.
- This is common in e-commerce websites with thousands of products or content-heavy blogs.
- Unoptimized loops, excessive object creation, or inefficient database queries can drain memory quickly.
- Some plugins and poorly coded themes in WordPress may consume excessive resources.
- In WordPress, installing too many plugins can overload the system and cause WordPress PHP out of memory fix issues.
- Laravel applications with multiple middleware, dependencies, and background processes can also trigger this error.
- Many shared hosting providers impose strict memory limits, making it harder to increase PHP memory limit beyond a certain point.
- Upgrading to a VPS or dedicated server may be necessary for high-traffic websites.
Real-World Examples of PHP Out of Memory Issues
Here are some common scenarios where developers face this error:
- WordPress: Trying to update multiple plugins or upload large media files results in an "Allowed memory size exhausted" error.
- Laravel: Running a heavy background job, such as data migration, exceeds the available memory limit.
- Magento: Processing large orders or generating reports causes the script to crash due to insufficient memory.
Now that we understand the root causes, let’s move on to how to fix PHP Fatal Error: Out of Memory and prevent it from happening in the future.
How to Fix PHP Fatal Error: Out of Memory
Now that we understand the causes of the PHP memory exhausted error, let’s go through different ways to fix it. One of the most effective methods is increasing the PHP memory limit to allocate more resources for your script.
1. Increase PHP Memory Limit
The PHP memory limit determines how much memory a script can use before it is forcibly terminated. If a script exceeds this limit, you’ll encounter the PHP Fatal Error: Out of Memory message. To resolve this, you need to manually increase the memory_limit value in PHP’s configuration files.
How to Modify the memory_limit
Value in
php.ini
The php.ini
file is the main PHP configuration file
where memory settings are defined. To increase the
PHP memory limit, follow these steps:
Locate the php.ini
File
/etc/php/{version}/apache2/php.ini (Linux)
C:\xampp\php\php.ini (Windows with XAMPP)
On a live web server, check the hosting control panel or use
phpinfo()
to find the location.Edit the php.ini
File
Open the file in a text editor and look for this line:
memory_limit = 128M
memory_limit = 512M
This allows PHP scripts to use up to 512MB of memory, reducing the chances
of running into a PHP script runs out of memory error.Restart the Web Server
If you’re using Apache, restart it with:sudo service apache2 restart
For Nginx and PHP-FPM, restart with:
sudo service php-fpm restart
sudo service nginx restart
The Importance of Setting an Appropriate Memory Limit
While increasing memory allocation can help, setting it too high may lead to excessive server resource usage. A good practice is:
- 128MB – 256MB for standard websites and blogs
- 512MB – 1GB for e-commerce sites, large applications, or WordPress with many plugins
- 1GB+ for heavy processing applications like Magento or custom-built software
Example: Increasing Memory for WordPress and Laravel
For WordPress Users
If you can’t access php.ini
, you can modify the
wp-config.php
file:
define('WP_MEMORY_LIMIT', '512M');
This is useful for resolving WordPress PHP out of memory fix issues, especially when dealing with plugins or media uploads.
For Laravel Developers
In Laravel, you may need to adjust the
php.ini
file or define memory limits dynamically
in the script:
ini_set('memory_limit', '512M');
This helps prevent PHP memory exhausted error when processing large data sets or running artisan commands.
By increasing the PHP memory limit, you can fix most cases of PHP Fatal Error: Out of Memory. However, if the issue persists, there are other methods to explore in the next section.
2. Modify .htaccess File
If you don’t have direct access to php.ini
or if
you’re using shared hosting where server configurations are restricted, you
can increase the PHP memory limit by modifying the
.htaccess
file.
This method is useful for WordPress, Laravel, and other PHP-based applications running on Apache servers.
How to Increase PHP Memory Using .htaccess
The .htaccess
file is a configuration file used by
Apache servers to control various settings, including memory allocation.
Follow these steps to increase the PHP memory limit using
.htaccess
:
Locate the .htaccess
File
public_html
for shared hosting or
/var/www/html
for VPS).If you don’t see it, enable "Show Hidden Files" in your file manager
or create a new file named .htaccess
.
Edit the .htaccess
File
Open the file in a text editor and add the following line:
php_value memory_limit 512M
This increases the PHP memory limit to 512MB, helping to prevent PHP memory exhausted error issues.
Save and Upload the File
- Save the changes and upload the file if editing locally.
- If you’re using an FTP client, make sure to transfer it in ASCII mode.
When to Use This Method and Its Limitations
Use This Method If:
-
You are on shared hosting and can’t modify
php.ini
. -
Your hosting provider allows
.htaccess
overrides. - You need a quick fix for PHP script runs out of memory errors in WordPress, Laravel, or other PHP applications.
⚠️
Limitations of .htaccess
for Increasing PHP Memory
Limit:
- Some hosting providers disable the php_value directive, meaning this method won’t work.
-
If
.htaccess
changes cause a 500 Internal Server Error, your server might not support it. -
Increasing the memory limit in
.htaccess
is less reliable than modifyingphp.ini
or using a direct PHP script.
If this method doesn’t work, you can try increasing PHP memory using wp-config.php (for WordPress) or modifying the server configuration (for VPS users).
3. Adjust PHP Memory via wp-config.php (For WordPress Users)
If you’re running a WordPress site and experiencing the
PHP Fatal Error: Out of Memory issue, one of the simplest
ways to fix it is by modifying the
wp-config.php
file. This method allows you to
increase the PHP memory limit without accessing
php.ini
or .htaccess
.
Step-by-Step Guide to Increasing Memory Limit in
wp-config.php
Locate the wp-config.php
File
-
The
wp-config.php
file is located in the root directory of your WordPress installation (e.g.,public_html
orwww
). - You can access it using an FTP client, cPanel File Manager, or a terminal (for VPS users).
wp-config.php
File-
Open the file with a text editor and look for the line:
/* That's all, stop editing! Happy publishing. */
-
Just above this line, add the following code:
define('WP_MEMORY_LIMIT', '512M');
- This tells WordPress to increase the PHP memory limit to 512MB, preventing PHP memory exhausted error issues.
- If using cPanel, save the file directly.
- If using FTP, upload the modified file back to the server.
-
You can check if the memory limit has increased by installing a plugin
like WP Server Info or running this code inside a
custom PHP file:
echo 'Memory Limit: ' . ini_get('memory_limit');
-
If the output shows
512M
, the changes have been successfully applied.
When and Why WordPress Users Should Apply This Fix
Use This Method If:
- You are experiencing WordPress PHP out of memory fix issues, such as white screen of death (WSOD) or plugin crashes.
-
Your hosting provider allows modifications to
wp-config.php
. -
You don’t have access to
php.ini
or.htaccess
.
⚠️ Limitations of This Method:
-
Some shared hosting providers override
wp-config.php
settings, meaning this fix may not work. - If the issue persists after applying this fix, you may need to contact your hosting provider to manually increase the memory allocation.
This method is an effective and WordPress-friendly way to resolve PHP memory exhausted error problems without requiring advanced technical skills.
4. Use a Custom PHP Script to Allocate More Memory
If you don’t have access to php.ini,
.htaccess
, or wp-config.php
,
you can use a custom PHP script to manually allocate more
memory. This method allows you to check the
current PHP memory limit and dynamically increase it when
needed.
Creating a Simple Script to Check the Current PHP Memory Limit
Before increasing the PHP memory limit, it’s important to check how much memory is currently allocated. You can do this by creating a simple PHP script:
-
Open a text editor and create a new PHP file (e.g.,
check_memory.php
). -
Add the following code:
<?php echo 'Current PHP Memory Limit: ' . ini_get('memory_limit'); ?>
- Save and upload the file to your server.
-
Open the file in your browser (e.g.,
https://yourwebsite.com/check_memory.php
).
This will display the current PHP memory limit, helping you determine whether it needs to be increased.
How to Allocate More Memory Dynamically
If your script is running out of memory, you can manually increase the PHP memory limit by adding this line inside your PHP file:
ini_set('memory_limit', '512M');
For example, if you have a script that processes large data files or complex calculations, modify it like this:
<?php
ini_set('memory_limit', '512M'); // Increase memory limit to 512MB
// Example function that consumes a lot of memory
$data = array_fill(0, 1000000, 'Testing Memory Usage');
echo 'Memory successfully increased!';
?>
After saving and running this script, your PHP script runs out of memory issue should be resolved.
When to Use This Method
Use This Method If:
- You need a temporary fix for a script that is exceeding the PHP memory limit.
-
You are on a VPS or dedicated server where
ini_set()
is allowed. - You want to increase memory limit dynamically within a specific PHP script instead of globally.
⚠️ Limitations of This Method:
-
Some shared hosting providers restrict the use of
ini_set('memory_limit', 'X');
, so this method may not work in all environments. - This change only affects the script where it is applied, not the entire server configuration.
Using a custom PHP script is a flexible way to fix PHP memory exhausted error, especially if you have limited access to server configurations.
5. Upgrade to a Hosting Plan with More Resources
If you have tried increasing the PHP memory limit but still encounter the PHP Fatal Error: Out of Memory, the problem may not be with your configuration but rather with the limitations of your hosting plan.
Many shared hosting providers impose strict memory limits, which can cause your PHP script to run out of memory, especially for resource-intensive applications like WordPress, Laravel, or eCommerce sites.
Why Shared Hosting Might Cause PHP Script Runs Out of Memory Issues
Shared hosting is the most affordable option, but it comes with limitations:
- Hosting providers set low memory limits (e.g., 128MB or 256MB) to prevent one website from using excessive resources.
- If multiple websites on the same server experience high traffic, your available memory may be reduced.
-
Some shared hosts override custom memory settings,
preventing you from increasing the PHP memory limit via
php.ini
,.htaccess
, orwp-config.php
.
If you are running a high-traffic website or using heavy plugins (e.g., WooCommerce, Elementor, or data-intensive applications), shared hosting may not provide enough resources, leading to frequent PHP memory exhausted error problems.
When to Consider Upgrading to a VPS or Dedicated Server
If your website continues to hit memory limits, upgrading to a Virtual Private Server (VPS) or dedicated server can be a long-term solution. Here’s when you should consider upgrading:
Upgrade to a VPS If:
- Your website receives consistent high traffic.
- You run complex PHP applications that require more resources.
- You need more control over server settings, including increasing PHP memory limit beyond shared hosting restrictions.
Upgrade to a Dedicated Server If:
- You manage multiple large websites and require dedicated resources.
- You need custom server configurations for performance optimization.
- You are experiencing persistent PHP script runs out of memory errors, even after trying other fixes.
Choosing a Hosting Provider That Supports Higher PHP Memory Limits
If you decide to upgrade, choose a provider that allows:
-
Customizable PHP configurations (
php.ini
andhtaccess
modifications). - Higher memory allocation (512MB or more for demanding applications).
- Scalability to accommodate future website growth.
Popular hosting providers that offer flexible PHP memory limits include:
- VPS Hosting: DigitalOcean, Linode, Vultr, Kamatera
- Managed WordPress Hosting: Kinsta, WP Engine, Cloudways
- Dedicated Servers: Liquid Web, A2 Hosting, HostGator
By upgrading to a better hosting plan, you can eliminate PHP memory exhausted errors permanently, ensuring a smooth and stable website experience.
4. Troubleshooting: When Increasing Memory Limit Doesn’t Work
Sometimes, even after increasing the PHP memory limit, you may still encounter the PHP Fatal Error: Out of Memory issue. If this happens, there are a few additional debugging steps you can take to diagnose and resolve the problem.
Checking Error Logs for Deeper Insights
One of the first steps in troubleshooting is to check your PHP error logs. These logs provide detailed information about what’s causing the PHP memory exhausted error.
Here’s how to check the logs:
For cPanel Users:- Log in to your cPanel.
- Navigate to Errors or File Manager > public_html > error_log.
- Open the log file and look for lines mentioning out of memory.
- Connect to your server using SSH.
-
Run the following command to check recent PHP errors:
(Replacetail -n 50 /var/log/apache2/error.log
apache2
withnginx
if using Nginx.) - Look for memory-related errors to identify the root cause.
If the error log shows repeated memory exhaustion issues, it might indicate that your PHP script is inefficient or your hosting resources are too limited.
Optimizing PHP Scripts to Reduce Memory Usage
If increasing the PHP memory limit doesn’t work, your scripts may be consuming too much memory due to inefficient coding. Here’s what you can do:
- Optimize Database Queries – Reduce heavy MySQL queries that may overload the memory.
- Use Pagination – If your script processes large datasets, paginate the results instead of loading everything at once.
- Uninstall Unnecessary Plugins (For WordPress Users) – Some plugins consume excessive memory, especially page builders or analytics tools.
- Free Up Memory in PHP Scripts – Manually unset large arrays after use:
unset($largeArray);
gc_collect_cycles(); // Forces garbage collection
Contacting Hosting Support for Assistance
If none of the PHP memory limit fixes work, your hosting provider may have hard limits on memory usage. In this case, it’s best to contact their support team and ask:
- What is the maximum PHP memory limit allowed on my plan?
- Can I increase the limit beyond the default settings?
- Are there server-side restrictions preventing me from changing the memory limit?
Some shared hosting providers restrict memory allocation, meaning you may need to upgrade to a VPS or dedicated server (as discussed in section 5).
By following these troubleshooting steps, you can determine whether the PHP Fatal Error: Out of Memory is due to a misconfiguration, a coding issue, or hosting limitations.
5. Conclusion
Dealing with a PHP Fatal Error: Out of Memory can be frustrating, but with the right approach, you can resolve the issue and prevent it from happening again.
Throughout this guide, we’ve covered why PHP memory exhaustion occurs, how to increase PHP memory limits, and what to do if those fixes don’t work.
Key Takeaways
- Understand the Causes – This error happens when a PHP script exceeds the allocated memory limit, often due to heavy plugins, large database queries, or inefficient coding.
- Increase PHP Memory Limit – Modify
php.ini
,.htaccess
, orwp-config.php
to allocate more memory. For WordPress, a simple edit inwp-config.php
can often fix the issue. - Check Hosting Limitations – Shared hosting plans may restrict PHP memory allocation, so upgrading to VPS or a dedicated server might be necessary for high-traffic websites.
- Optimize Your Code – Reduce memory usage by optimizing queries, using pagination, and freeing up unused variables in your PHP scripts.
- Monitor Error Logs – Regularly check PHP error logs to identify recurring memory issues and debug them efficiently.
Best Practices to Prevent PHP Fatal Error: Out of Memory in the Future
To ensure your website or application runs smoothly without PHP memory exhausted errors, follow these best practices:
- Choose a Hosting Plan That Supports Higher Memory Limits – If you run a resource-intensive website, avoid low-tier shared hosting.
- Use Lightweight Plugins & Themes – Avoid bloated plugins that consume excessive memory.
- Optimize Database Queries – Reduce the number of large or unnecessary queries.
- Enable Caching – Use caching solutions to minimize repeated script execution.
- Regularly Update PHP & Scripts – Newer PHP versions often have better memory management.
By implementing these strategies, you can maintain a stable PHP environment and prevent unexpected out-of-memory crashes, keeping your website or application fast and reliable.