How to Fix “Allowed memory size exhausted” in Laravel and WordPress on Shared Hosting
On shared hosting it is very common to see an error like: PHP Fatal error: Allowed memory size of 268435456 bytes exhausted In Laravel it usually appears in the log: storage/logs/laravel.log In WordPress you may get a white screen or a message in the browser saying “Allowed memory size exhausted“.
On shared hosting you do not have full server control, but you still have several safe ways to fix it. Let’s go step by step.
1. Understand what the error means
PHP gives each script a maximum amount of memory. When your code or a plugin tries to use more than that limit, PHP stops execution and throws this fatal error.
Default limits on cheap shared hosting are often only 64M or 128M, which is not enough for:
- Heavy Laravel jobs or imports
- Large WordPress plugins
- Big WooCommerce catalogs
- Image processing, backups, or page builders
The fix is usually a mix of:
- Increasing the memory limit
- Reducing what the script tries to do at once
2. Check the current PHP memory limit
Create a file called phpinfo.php in your public folder:
<?php phpinfo();
Open it in the browser and look for:
memory_limit
You might see something like 128M. That is the maximum memory every PHP request can use.
Delete phpinfo.php after checking, for security.
3. Increase memory for Laravel on shared hosting
3.1 Using .htaccess (Apache based shared hosting)
In your Laravel public/.htaccess file, add:
php_value memory_limit 512M
If your host allows overriding PHP values in .htaccess, this immediately raises the limit.
If you get a server error after adding it, your host might block this directive. Simply remove that line again.
3.2 Using php.ini or user.ini
Some hosts allow a custom php.ini or .user.ini.
Try creating a file named php.ini in your document root:
memory_limit = 512M
Or .user.ini:
memory_limit = 512M
Wait a minute and refresh your Laravel page. You can confirm with phpinfo() again.
3.3 Setting memory limit in Laravel entry point
As a last shared-hosting friendly option, you can set the limit from PHP itself.
In public/index.php, near the top:
ini_set('memory_limit', '512M');
This only affects that Laravel application and not other sites on the same account.
4. Increase WordPress memory limit correctly
WordPress has its own constants for memory.
4.1 Edit wp-config.php
Open wp-config.php and add above the line that says “That’s all, stop editing!”:
define( 'WP_MEMORY_LIMIT', '256M' );
define( 'WP_MAX_MEMORY_LIMIT', '512M' );
WP_MEMORY_LIMIT is for frontend requests. WP_MAX_MEMORY_LIMIT is for admin tasks like backups and imports.
4.2 Use .htaccess as a fallback
In the WordPress root .htaccess (same folder as wp-config.php):
php_value memory_limit 512M
Again, if the server throws a 500 error, remove this line and rely on wp-config.php plus any hosting panel settings.
5. Find what is actually eating your memory in Laravel
If the error appears only in specific actions, you may have inefficient code.
5.1 Large queries and collections
Avoid loading huge tables into memory:
// Risky
$users = User::all();
// Better
User::chunk(1000, function ($users) {
foreach ($users as $user) {
// process
}
});
5.2 Heavy debug logging
In config/app.php, make sure APP_DEBUG is false in production. Too much logging can grow files quickly and use memory when writing.
In .env:
APP_ENV=production
APP_DEBUG=false
5.3 Queues and jobs on shared hosting
If you run jobs with php artisan queue:work in a long loop, memory can grow over time.
Use:
php artisan queue:work --max-jobs=100 --max-time=300 --memory=256
This tells the worker to stop before it becomes too big.
6. Reduce memory usage in WordPress
Even with a higher limit, some plugins can easily consume everything.
6.1 Deactivate heavy plugins
In the WordPress admin, temporarily disable:
- Backup plugins
- Security scanners
- Page builders not in use
- Image optimization plugins
Then retry the action that caused the error.
If the site is completely broken, rename the /wp-content/plugins/plugin-name folder via FTP to deactivate it.
6.2 Optimize images
Large uncompressed images can cause memory issues during resizing.
Use tools like TinyPNG or Imagify before uploading, and limit max image dimensions.
6.3 Limit WooCommerce and query heavy pages
On big product catalogs, avoid loading thousands of posts at once with custom queries. Use pagination and lazy loading whenever possible.
7. Ask your shared host for a higher limit
Some providers impose a hard cap, for example 256M. If you keep hitting the error even after optimizing, open a ticket and ask:
- What is the maximum memory_limit for my plan?
- Can you increase it to 512M for this domain?
If they cannot, it might be time to move the site or Laravel app to a small VPS where you control PHP settings.
8. Quick checklist
- Check the current
memory_limitwithphpinfo(). - Raise PHP memory using
.htaccess,php.ini, or.user.ini. - For WordPress, set
WP_MEMORY_LIMITandWP_MAX_MEMORY_LIMITinwp-config.php. - Optimize Laravel code with chunked queries and safe queue options.
- Disable or replace heavy WordPress plugins.
- Contact your host if you hit a fixed upper limit.
Once you balance a reasonable memory limit with better code and plugins, the “Allowed memory size exhausted” error becomes rare, even on shared hosting.