PHP Imagick内存泄漏 [英] PHP Imagick memory leak
问题描述
我必须在PHP CLI上使用Imagick渲染某些内容. 我注意到服务器内存每隔3-5天就会用完,所以我什至无法通过ssh或ftp连接.
I have to render something with Imagick on PHP CLI. I have noticed that every 3-5 days the server memory gets full, so i can't even connet via ssh or ftp.
使用memory_get_usage()将内存泄漏归结为脚本的imagick部分. 脚本看起来像这样:
with memory_get_usage() i narrwoed the memory leak down to the imagick part of the script. the script looks something like this:
$sourceImg = 'source.png';
$destImg = 'dest.png';
$background ='#00ff00';
$im = new Imagick();
$im->pingImage($sourceImg);
$im->readImage($sourceImg);
$draw = new ImagickDraw();
for($i=1;$i<=5;$i++){
$draw->setFillColor( $background);
$draw->rectangle( 10*$i+5, 10, 10*$i+10, 20);
}
$im->drawImage( $draw );
$im->writeImage( $destImg );
$im->destroy();
unset($im,$draw);
我销毁图像参考,并取消设置imagick和imagickDraw对象,但是脚本不会释放任何内存. setFillColor()方法占用最多的内存
I destroy the image reference, and unset the imagick and imagickDraw object, but the script won't release any memory. The setFillColor() method takes the most memory
我还能做些其他事情来释放imageick占用的空间吗?
Can i do something else to free the space used by imageick?
推荐答案
imagick使用共享库,并且它的内存使用情况对于PHP来说是遥不可及的,因此调整PHP内存和垃圾回收将无济于事.
imagick uses a shared library and it's memory usage is out of reach for PHP, so tuning PHP memory and garbage collection won't help.
我自己也遇到了同样的问题,试图处理50页(!)3000x2000像素的多页tiff图像. 解决方案是让imagick将其像素缓存放在磁盘上.
I had the same problem myself, trying to handle a multi-page-tiff image with 50 (!) pages of 3000x2000 pixels. The solution is to have imagick put its pixel cache on disk.
在创建Imagick
对象之前添加此内容对我来说解决了这个问题:
Adding this prior to creating the Imagick
object solved the problem for me:
// pixel cache max size
IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 256);
// maximum amount of memory map to allocate for the pixel cache
IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, 256);
目标是使imagick将其像素缓存放置在磁盘上,而不是在RAM中.默认位置似乎是文件/tmp/magick-XXnnnnn,因此请确保/tmp不在shmfs/ramdisk上,或者更改imagick使用的临时目录.
The goal is to make imagick put its pixel cache on disk instead of in RAM. The default place seems to be files /tmp/magick-XXnnnnn, so make sure /tmp is not on shmfs/ramdisk, or change the temp directory imagick uses.
要研究的其他资源限制:imagick::RESOURCETYPE_DISK
,imagick::RESOURCETYPE_FILE
和imagick::RESOURCETYPE_AREA
.
它们在 imagick :: getResourceLimit()手册页中进行了描述(在页面中效果不太理想)表示setResourceLimit()
).
Other resouce limits to investigate: imagick::RESOURCETYPE_DISK
, imagick::RESOURCETYPE_FILE
, and imagick::RESOURCETYPE_AREA
.
They are described in the imagick::getResourceLimit() manual page (not so well in the page for setResourceLimit()
).
在我的图像处理循环中,我有set_time_limit(300)
,因为脚本需要花费很多时间来处理这张巨大的图像(当解压缩时).
In my image handling loop, I have set_time_limit(300)
, since the script takes ages to process this huge (when uncompress) image.
在最新版本中,不应将
setResourceLimit()
作为静态方法调用,而应在实际对象上调用,例如:
EDIT : In recent versions,
setResourceLimit()
should not be called as a static method, but on the actual object instead, such as:
$im->setResourceLimit(imagick::RESOURCETYPE_MEMORY, 256);
$im->setResourceLimit(imagick::RESOURCETYPE_MAP, 256);
$im->setResourceLimit(imagick::RESOURCETYPE_AREA, 1512);
$im->setResourceLimit(imagick::RESOURCETYPE_FILE, 768);
$im->setResourceLimit(imagick::RESOURCETYPE_DISK, -1);
这篇关于PHP Imagick内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!