Home / Blog / Web Development

Web Development

Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted (PHP Fix)

This PHP error means a script used more memory than allowed. Raising the limit hides the problem. Fixing the cause solves it.

In this article
  1. The error
  2. The wrong fix
  3. Root cause
  4. The better fix
  5. When to raise it
  6. Frequently asked questions

"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.

The Ucodice Team is a group of passionate developers, designers and strategists delivering web, mobile and software solutions to clients worldwide.

Ready to take the next step?

Explore our web development services or tell us about your project. A senior member of our team will get back to you with clear next steps.

Let's start your project

Tell us what you want to build or grow. We'll reply within one business day with clear next steps.

Phone: 9560430495  ·  Email: contact@ucodice.com

Office: Ucodice Technologies, IHDP Business Park, Street Number 7, Sector 127, Noida, Uttar Pradesh 201304. Also serving Delhi NCR and clients worldwide.

Trusted and certified

  • Google PartnerCertified for Google Ads
  • Meta business PartnerCertified for Meta Ads
  • 4.3 / 5 on Google47 Google reviews
  • 4.9 / 5 on Freelancer.in245 reviews

Free consultation

Tell us about your project.

Get a free quote