phpmemory-leaksapache2imagick

Problem of memory leak with PHP and queryFontMetrics from Imagick


I've been using Imagick for years for my projects. And for the past few days, I've been having memory problems. My PHP code using queryFontMetrics, and at the end of the execution, it does not free the memory. And after several executions, the server memory is full, and I have to restart Apache to free the memory.

I created a little code to test:

gc_enable();

$draw = new ImagickDraw();
$draw -> setFont($fontFile);
$draw -> setFontSize($fontSize);

$imagick = new Imagick();
$bbox = $imagick -> queryFontMetrics($draw,$text);  

$draw->clear();
$draw->destroy();
$imagick->clear();
$imagick->destroy();
unset($draw,$imagick);
gc_collect_cycles();

return $bbox;

I run this code in a loop with 1000 iterations When I run this code on the php command line, no problem, the memory is completely freed. But when I run it with Apache, it doesn't free the memory. I tested this code on a debian 12 server with php 8.2, apache2 2.4.59, Imagick 3.7.0 and ImageMagick 6.9.11-60.

Is there something I'm doing wrong? Thanks for your help !


Solution

  • After several searches, I have tried to change the version of imagemagick, and it solved the problem. So if you have this problem check your imagemagick version, for me it was version 6.9.11-60.