When calling Symfony's Uploadfile->getMimeType() on a csv file with 1100 rows and 60 columns and ~400kb size, $finfo->file tries to use around 5gb memory which gives me the following error:
Fatal error: Allowed memory size of 1077936128 bytes exhausted (tried to allocate 4294937911 bytes) in /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php on line 69
This is the part of code that gives that error:
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
return;
}
return $finfo->file($path);
Btw this happens on our production server that has PHP version 5.4, but doesn't happen on my dev server with php 7.
There is a bug in php 5.4 (already fixed in 2015, so you really should upgrade the php version in your server) that cause finfo::file function to allocate huge amounts of memory when trying to extract the info of a csv file.
Here is the link to the bug:
https://bugs.php.net/bug.php?id=69224
Related
I have a php script that keeps throwing an error on the following line
base64_encode(serialize(array($data, $context)))
Here is the error
Out of memory (allocated 471859200) (tried to allocate 234607507
bytes)
I have added the following two line before calling the base64_encode() method
set_time_limit(0);
ini_set('memory_limit', '-1');
As you can see, I am giving the script all the available physical memory (16GB)
What could be causing this error?
PHP produces two memory-related error messages:
When memory_limit is exceeded, e.g.:
Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)
When PHP is unable to get more memory from the OS:
Out of memory (allocated %zu) (tried to allocate %zu bytes)
… with minor wording variations depending on the exact context (see PHP source code at Zend\zend_alloc.c for details).
Using a simile, you can exhaust your disk quota or the disk can fill up.
The second situation is basically a crash and is relatively easy to find if you run memory-intensive tasks in a 32-bit PHP process. There's no fixed rule but when your needs get close to 1GB bad things happen. If you determine that you're running 32-bit PHP (the OS architecture doesn't matter), and even if you don't, you may want to consider a rewrite.
I have no idea about your use case so these are only vague tips:
I presume you base64_encode() your data to submit it elsewhere. Perhaps you don't need to hold the entire data in memory and you can decode it in chunks and send it as you go or store it in a file.
You can replace serialize() with an alternative implementation that operates in chunks or switch to another format.
The original installation was on Ubuntu 14.04 with PHP 5.5.9 and CakePHP 5.4.1. After migrating to Ubuntu 16.04 with PHP 7.0.30 I updated CakePHP to 2.10.4 mainly by exchanging the lib folder. Everything works fine in the web interface, even the memory consuming tasks of the application.
But the CLI is broken. Every call to 'app/Console/cake' throws a 'PHP Fatal error'.
root#watt18:/var/www_external/app# Console/cake bake
PHP Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 8192 bytes) in /var/www_external/lib/Cake/Console/ConsoleOptionParser.php on line 510
PHP Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 32768 bytes) in /var/www_external/lib/Cake/I18n/I18n.php on line 1
The 'in' part changes from call to call. Searching the web I could not find any comparable topic. I checked the persmissions on all the folders inside 'app' but could not find any differences to the original installation. I tried to debug but couldn't get really far yet. Up to the line return ShellDispatcher::run($argv); in Console/cake.php everything is fine.
I get the same result when making a call inside the original cakephp folder from a fresh download.
I have no idea where all the memory gets lost.I would be happy about any hint to what might cause the problem or how to continue with debuging.
set memory_limit = -1 in your php.ini
but this is not a good solution , you may have a memory leake somewhere
please double check your statements , specialy if there is a loop with many memory usage
Hello i am facing the problem of Allowed memory size error. I have created the project in codeigniter, php and I have used Tbs library. Its worked fine but today it display the error:
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 258010 bytes) in
/home/abc/public_html/application/libraries/Tbs.php on line
4222
Please give me any suggestions.
If you are using TBS + OpenTBS in order to merge an XLSX file , then there is a known problem fixed with OpenTBS version 1.9.2 : if the number of row to merge is quite numerous, then you can have a very long process or a memory size error when calling $TBS->Show().
Use OpenTBS 1.9.2 or higher which is optimized for this point, and if the process is still long you can optimize more using the OPENTBS_RELATIVE_CELLS command.
"Allowed memory size of XXXXXX bytes exhausted" is a typical error when you don't have, as it's own name says, enough memory available for the php query to run.
Try upgrading memory_limit variable on your php.ini file, or by setting up ini_set('memory_limit', 'XXXM'); on your PHP file that you are running, being XXX the amount of Mb memory that you want to define.
If this isn't the case, you might have a bug in your software that is causing, i.e. a loop that is consuming memory without any control; but as you have said that it did work before...
Anyway, please check all the other answers from StackOverflow on this aspect:
Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)
CodeIgniter Fatal error: Allowed memory size of bytes exhausted
Codeigniter - Allowed memory size exhausted while uploading
Generally this type of occurs when your script is using too much memory. This can often happen in PHP if you have a loop that has run out of control and you are creating objects or adding to arrays on each pass of the loop. Check for infinite loops.
If that isn't the problem, try and help out PHP by destroying objects that you are finished with by setting them to null eg. $OldVar = null;.
Check the code where the error actually happens as well. Would you expect that line to be allocating a massive amount of memory? If not, try and figure out what has gone wrong.
Never upgrad memory_limit variable on your php.ini file, or by setting up ini_set('memory_limit', 'XXXM'); on your PHP file that you are running, being XXX the amount of Mb memory that you want to define.Letting application to eat memory he wants to is insane step.Try to find out bug does your application wants that much memory?? Find out
I ran into this rather annoying issue the other day; when a page tries to load it will just 'stop' half way through returning half a page to the end user. The exact error is shown below.
[03-Jul-2015 03:15:04 Europe/London] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 18996 bytes) in [path]/concrete/vendor/imagine/imagine/lib/Imagine/Gd/Image.php on line 602
I spoke to my host and they bumped up PHP's memory to 512M (which is a bit overkill) yet it still throws this error. I'm not sure if it's a memory leak in Concrete5 somewhere or if it is just the server that the site is hosted on.
Any suggestions are much appreciated.
Edit: Forgot to mention I'm running 1.7.4.2
Edit 2: For reference the exact function is imagecreatetruecolor()
Either:
ini_set('memory_limit','256M');
Inside PHP or getting your host to bump up the memory limit will do the trick.
I'm under Win2008R2 and using odbc 32bit.
By switching from PHP 5.5.14 to 5.6.4 many simple queries now hangs with:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate ... MORE THEN 1GB ...)
Some of the queries are very simple and should load just some record or sometimes NULL.
I've read in other posts that if some field of the returned records are NULL this could lead to errors, but I can't solve by correcting the queries, these are thousands in a very big non standard enviroment!!
For Windows there is a bug report, have a look
https://bugs.php.net/bug.php?id=68964