Large image folder crashes Symfony "cache:clear" with OutOfMemoryException

890 Views Asked by At

When I put a large image folder in my /web folder, the "cache"clear" command fails with a

"OutOfMemoryException"

PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 151552 bytes) in /Users/john/Development/git/website/vendor/twig/twig/lib/Twig/Compiler.php on line 124

[Symfony\Component\Debug\Exception\OutOfMemoryException]                                  
Error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 151552 bytes)  

Exception trace:
() at /Users/john/Development/git/website/vendor/twig/twig/lib/Twig/Compiler.php:124

The image folder contains 10.000 images and is about 600MB big. When I delete this folder, the "cache:clear" command runs successfully.

I'm using Symfony 2.8 with PHP 7.1. The PHP memory limit is set on 512MB, which is the maximum on my hosting.

Update:

When I add --no-warmup to the command, I don't get the exception.

$ php app/console cache:clear --verbose --no-warmup
2

There are 2 best solutions below

1
Jasson Rojas On BEST ANSWER

You can limit the usage of the php command like this:

php -d memory_limit=512M app/console cache:clear --verbose --no-warmup

Hope this works for you :)

3
Matthijs Otterloo On

What you could do is changing the memory_limit by ini_set('memory_limit', '-1'); but this is NOT a solution at all.

Please don't do that. Obviously php has a memory leak somewhere and you are telling the server to just use all the memory that it wants. The problem has not been fixed at all. If you monitor your server, you will see that it is now probably using up most of the RAM and even swapping to disk.

You should probably try to track down the exact bug in your code and fix it.