In this article
"Fatal error: Allowed memory size of 33554432 bytes exhausted" means a PHP script tried to use more than its 32 MB limit. On shared hosting, where ini_set is often disabled, you cannot simply raise the limit, so the real fix is to find out why the script uses so much memory.
Key takeaways
- 33554432 bytes is 32 MB, a common default memory_limit.
- Raising the limit only lets a wasteful script use more memory.
- Large data kept in a variable inside a loop is a common cause.
- Free memory with unset() or null, or process data in chunks.
The fatal error
A script in one of our applications stopped with this error. Research showed that the script was consuming all of its available memory. Because the site was on shared hosting where the admin had disabled ini_set, changing the limit was not an option, so we had to find the cause.
The wrong fix: only increasing memory
Raising memory_limit in php.ini is the quick answer you will find first. It only allows the problem script to use more memory, and it does not explain why it uses so much. Use it as a short-term measure, not as the solution.
memory_limit = 256M
The root cause: large data kept in memory
Repeatedly loading large amounts of data into the same variable inside a loop is a frequent cause. The variable stays in use, so PHP cannot free its memory even as the value changes.
The better fix: release memory
Once you have finished with a large variable, release it so PHP can reclaim the memory:
$large_variable = null;
// or
unset($large_variable);
- Find the variable that holds large amounts of data.
- After using the data, set it to null or unset() it.
- Process big result sets in chunks or with generators instead of loading everything at once.
- Select only the columns you need from the database.
When raising the limit is fine
If the script legitimately needs more memory, such as image processing or a large export, raise memory_limit through php.ini, .htaccess or your hosting panel. Do it after you have removed obvious waste. Need help with a slow or failing PHP application? See our web development services.
Frequently asked questions
How many MB is 33554432 bytes?
It is 32 MB, a common default value for memory_limit.
Where do I change memory_limit?
In php.ini, in .htaccess on some hosts, or through your hosting control panel. On shared hosting you may need to ask your provider.
Is unset() better than setting null?
Both release the reference. unset() removes the variable, and null keeps it with an empty value. Either lets PHP free the memory when nothing else uses it.



