craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.27k stars 635 forks source link

[4.x]: Image transforms via queue - memory leak? #13098

Open aloco opened 1 year ago

aloco commented 1 year ago

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.

Screenshot 2023-04-19 at 11 15 17 Screenshot 2023-04-19 at 11 16 34
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 
Screenshot 2023-04-19 at 11 44 04

We are using this image: craftcms/cli:8.1@sha256:1f972a7493e056a21bcc4604c0dd44c497797d9fd2fecb61f5919c263228f2ab

Steps to reproduce

  1. Create Image Transforms
  2. Upload 20+ Images
  3. Watch Generating pending image transforms
  4. 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

angrybrad commented 1 year ago

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.

DavidStrada commented 2 months ago

@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

Screenshot 2024-08-21 at 6 14 41 PM Screenshot 2024-08-21 at 6 14 27 PM