98% of the time this error comes from loading more into memory than what you set up PHP to handle in one process. There are other causes, but these are much less common — very rarely it can be a memory leak if you're on PHP 5.3 and above.
If you aren't sure what your PHP memory limit is set to, it's helpfully included in the error message. The size is reported in bytes, though, so we've done some conversions for you:
- PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted - 8 MB
- PHP: Fatal Error: Allowed Memory Size of 16777216 Bytes Exhausted - 16 MB
- PHP: Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted - 32 MB
- PHP: Fatal Error: Allowed Memory Size of 67108864 Bytes Exhausted - 64 MB
- PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted - 128 MB
- PHP: Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted - 256 MB
- PHP: Fatal Error: Allowed Memory Size of 536870912 Bytes Exhausted - 512 MB
- PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted - 1 GB
What do I have to do to resolve it?
Your first course of action is to increase your memory limit. Note, this is a temporary debugging producedure. The goal is to increase the memory to a point where we have the application working again for the purpose of then reducing the memory usage. Once you decrease the memory usage you can lower the memory limit it to a value that's more suitable. Your plan should be to use as little memory as you could practically use where the application works and functions correctly in a production server based on the workload by your users (humans or programmatic). I usually recommend setting the memory limit to something high, like 1GB, assuming you have at least 150% of that free in RAM.
Also, never do these tests on a production server unless you're sure you have plenty of RAM and you fully understand how web server processes consume memory. You could easily bring a server to its knees if there are many concurrent processes running, each using a high amount of memory. I would never, ever recommend setting the memory limit to
-1
(unlimited) in a production environment. That's a recipe for disaster. Don't make that newbie mistake.
So how do you do this? Simple — increase the memory limit programmatically early on in your code, before your process runs out of memory. If you do it this way, you can give PHP extra memory only when that piece of code gets called rather than increasing the memory limit for all PHP processes.
<?php
ini_set('memory_limit', '1024M'); // or you could use 1G
- If by increasing the memory limit you have gotten rid of the error and your code now works, you'll need to take measures to decrease that memory usage. Here are a few things you could do to decrease it:
- If you're reading files, read them line-by-line instead of reading in the complete file into memory. Look at
fgets
andSplFileObject::fgets
. - Upgrade to a new version of PHP if you're using PHP 5.3. PHP 5.4 and 5.5 use much less memory.
- Avoid loading large datasets into in an array. Instead, go for processing smaller subsets of the larger dataset and, if necessary, persist your data into a database to relieve memory use.
- Try the latest version or minor version of a third-party library (1.9.3 vs. your 1.8.2, for instance) and use whichever is more stable. Sometimes newer versions of libraries are written more efficiently.
- If you have an uncommon or unstable PHP extension, try upgrading it. It might have a memory leak.
- If you're dealing with large files and you simply can't read it line-by-line, try breaking the file into many smaller files and process those individually.
- Disable PHP extensions that you don't need.
- In the problem area, unset variables which contain large amounts of data and aren't required later in the code.
https://www.airpair.com/php/fatal-error-allowed-memory-size
No hay comentarios:
Publicar un comentario