[4.x]: Image transforms via queue - memory leak?
What happened?
Description
Not sure if this is a bug or just a matter of configuration.
We are using image transforms for our assets, they are handled via queue. We noted, that the job Generating pending image transforms needs very long for 75 images and gets canceled due to queue ttr. We already set the limit to 6000 which is still not enough. I am aware, that image transforms are expensive and it´s ok that it needs some time, however, our actual issue is, that the job is not freeing up memory after transforming or checking an image. As soon as the job gets canceled (after 6000) the memory usage drops. To me, the memory usage chart looks like it keeps already transformed images open and moves to the next one. As soon as the job timed out, memory usage dropped back to normal.

cat /sys/fs/cgroup/memory/memory.stat
cache 96321536
rss 3781828608
rss_huge 0
shmem 0
mapped_file 20951040
dirty 0
writeback 270336
swap 0
pgpgin 37919805
pgpgout 36972963
pgfault 38191395
pgmajfault 924
inactive_anon 0
active_anon 3780919296
inactive_file 52199424
active_file 44879872
unevictable 0
hierarchical_memory_limit 14042316800
hierarchical_memsw_limit 9223372036854771712
total_cache 96321536
total_rss 3781828608
total_rss_huge 0
total_shmem 0
total_mapped_file 20951040
total_dirty 0
total_writeback 270336
total_swap 0
total_pgpgin 37919805
total_pgpgout 36972963
total_pgfault 38191395
total_pgmajfault 924
total_inactive_anon 0
total_active_anon 3780919296
total_inactive_file 52199424
total_active_file 44879872
total_unevictable 0
We are using this image: craftcms/cli:8.1@sha256:1f972a7493e056a21bcc4604c0dd44c497797d9fd2fecb61f5919c263228f2ab
Steps to reproduce
- Create Image Transforms
- Upload 20+ Images
- Watch
Generating pending image transforms - Memory Usage increases
Expected behavior
Memory on server should be freed, after an image within one job gets transformed before moving to the next image within the same job
Actual behavior
Memory on server grows until job get´s killed or server runs out of memory
Craft CMS version
4.2.0.2
PHP version
8.1.9
Operating system and version
Linux 5.4.144+
Database type and version
MySQL 10.5.13
Image driver and version
Imagick 3.7.0 (ImageMagick 7.1.0-35)
Installed plugins and versions
Blitz 4.2.1 CP Field Inspect 1.4.4 Empty Coalesce 4.0.0-beta.1 ImageOptimize 4.0.2 Redactor 3.0.2 SEOmatic 4.0.7 Sprig 2.3.0 Super Table 3.0.0 Typed link field 2.1.3-rc Vite 4.0.2
I’d strongly suspect it’s Imagick leaking memory over Craft since that’s kind of what it’s known for.
Try putting a https://imagemagick.org/script/security-policy.php in place with some resource restrictions (including memory). Imagick doesn’t respect any php.ini resource settings, so that’s the mechanism you use to control how much it has access to.
@angrybrad We are currently being hit by this same issue on a site: (2 queue jobs are needed in order to process different jobs used on the site).
There's very little information to help debug this fix, which makes it hard to provide more details to help you debug/fix this issue.
Craft CMS 4.10.5 Kubernetes GCP
any update on this, I am also experiencing similar issue with craft 5
I just generated over 1,000 images via the queue on Craft 4.15.6.2 and I’m not seeing any indication of a memory leak, either in PHP’s memory usage or ImageMagick’s.
Here’s PHP’s memory usage throughout the queue/run request (in MBs):
Typically when we see excessive resource usage for transforms, it’s due to a specific image that ImageMagick has trouble with, such as an animated GIF with hundreds of frames, or a PNG file that should have been a JPG.