PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 12352 bytes) in

PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 12352 bytes) in

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 12352 bytes) in /home...

This specific error message is a classic trap for developers managing high-load image upload endpoints. You might see the error while trying to process a single, massive file or during a burst of concurrent uploads. The stack trace points to a PHP Fatal error where memory allocation failed, specifically noting that 12352 bytes were requested but only 80740352 bytes (approx 77MB) were currently available in the process's heap.

Your current php.ini configuration looks suspiciously permissive at first glance. You have set upload_max_filesize and post_max_size to 2000M each, and max_file_uploads to 8. While these values dictate how much data PHP expects to receive, they do not directly control the amount of RAM the PHP process is allowed to occupy while executing code.

The discrepancy between your file size limits (2GB) and your memory allocation limit (77MB) is the root cause. When an image upload handler attempts to load a large binary file into memory for processing—perhaps using file_get_contents() or loading it directly into an array—the PHP engine tries to reserve contiguous memory blocks. If the process hits its hard ceiling, any subsequent attempt to allocate even a tiny chunk of memory triggers this fatal error.

Understanding Memory Limits in php.ini

To resolve this, you need to adjust the specific directives that govern the total memory available to the PHP interpreter. There are three primary settings to consider:

  1. memory_limit: This is the most critical setting for your specific error. It defines the maximum amount of memory a script may consume. If you have set this to 76M (which matches your allocated 80740352 bytes roughly), any operation requiring more will crash, regardless of how much disk space or network bandwidth is available. You should increase this value based on your server's RAM capacity. A common starting point for image-heavy applications is 256M or 512M, provided you have the physical RAM to support it.

  2. max_execution_time: Increasing memory limits without adjusting execution time can lead to scripts hanging indefinitely if they are processing slow images. If a script tries to load a massive file and fails memory checks, it might hang before failing. Ensuring max_execution_time is sufficient (e.g., 300 seconds) prevents premature timeouts that mimic memory errors.

  3. post_max_size vs. upload_max_filesize: Your current setup has both set to 2000M. While this handles the input stream size, it's worth noting that post_max_size must always be greater than or equal to upload_max_filesize. If you were to lower post_max_size below your file limit, uploads would fail silently with incomplete data, but since they are matched here, the configuration is consistent.

Implementation and Trade-offs

To implement the fix, edit your php.ini file (or create a dedicated .user.ini if using shared hosting) and update the memory directive:

memory_limit = 512M
upload_max_filesize = 2000M
post_max_size = 2000M
max_file_uploads = 8

After making changes, you must restart the web server or PHP-FPM service for the settings to take effect. In a shared hosting environment, this might require opening a ticket with the provider. On a self-hosted server, commands like systemctl restart php-fpm or service apache2 reload will apply the new limits immediately.

It is crucial to balance these settings against your physical RAM. If you are running multiple PHP processes (via Apache mod_php or a pool of FPM workers), setting memory_limit too high across all processes can lead to swapping, which degrades performance significantly. Monitor your server's memory usage after increasing the limit to ensure it doesn't exceed available resources.

If you are building an application where image processing is central to the business logic, consider leveraging a specialized tool like LaravelCompany for optimized image handling pipelines that minimize memory overhead by processing images in streams rather than loading entire files into RAM. However, regardless of the framework or library used, the underlying memory_limit in php.ini remains the hard ceiling that PHP scripts cannot exceed.

Our network