Warning: Undefined array key "rcommentid" in /home/data/websites/jnlin.org/webroot/wp-content/plugins/wp-recaptcha/recaptcha.php on line 348

Warning: Undefined array key "rchash" in /home/data/websites/jnlin.org/webroot/wp-content/plugins/wp-recaptcha/recaptcha.php on line 349
避免 PHP Imagick 把記憶體吃完… – Jui-Nan Lin's Blog

避免 PHP Imagick 把記憶體吃完…

PHP 的 Imagick 有 memory leak 的情形,當用 PHP-CGI 跑動態縮圖服務的時候,這個問題更加的嚴重。查了一下資料,可以設定參數限制使用的記憶體大小:


// set pixel cache max size to 256MB
IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 256 * 1024 * 1024);
// maximum amount of memory map to allocate for the pixel cache
IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, 256 * 1024 * 1024);

單位是 Bytes。如果進行圖片處理時,所需的記憶體超過指定值時,會在 /tmp 寫入暫存檔案。可以使用 MAGICK_TMPDIR 這個環境變數來指定暫存目錄:


putenv('MAGICK_TMPDIR=/tmp/imagick');

Leave a Reply

Your email address will not be published. Required fields are marked *


Warning: Undefined array key "rerror" in /home/data/websites/jnlin.org/webroot/wp-content/plugins/wp-recaptcha/recaptcha.php on line 291