thephpleague/mime-type-detection

Allowed memory size of 536870912 bytes exhausted (tried to allocate 792238184 bytes)

halaei opened this issue · 4 comments

Hi there,
I see there is a function that limits the memory usage of Mimetype detector. I think I am using this package through Laravel file system.
I am seeing this error in my logs for a few days and I think it didn't happened before. Do you know what changes in laravel or flysystem lead to to this memory exhaustion?

If a recent change lead to this error, I think maybe the default value of $bufferSampleSize should be a small integer (1MB for example) or maybe flysistem should construct the appropriate object with limited memory consumption.
Thanks for your help.

Here is how I am trying to fix. But there is another issue: MimeType is an internal class and IDE gives me a warning, meaning I shouldn't use it.

use League\Flysystem\Util\MimeType;
use League\MimeTypeDetection\FinfoMimeTypeDetector;

MimeType::useDetector(new FinfoMimeTypeDetector('', null, 1000000));

Also by reading error again I noted that PHP is trying to allocate 790MB. I am working of files less than 50MB. Why 790MB is needed to detect a file that is smaller than 50MB?

See https://bugs.php.net/bug.php?id=78987 and https://bugs.php.net/bug.php?id=79263
They say it is a libmagic bug. I reported it to ubuntu and https://github.com/file/file issue tracker. Let see what will happen next.

I am also having the same issue. my file size is 260 MB and trying to allocate 2+ GB.

There's nothing I can do to prevent this from this library. There is a way to sample the contents, but other than that it's a PHP bug, not a lib bug.