<?xml version="1.0" encoding="UTF-8" ?><oembed><version>1.0</version><provider_name>Jui-Nan Lin&#039;s Blog</provider_name><provider_url>https://jnlin.org</provider_url><author_name>jnlin</author_name><author_url>https://jnlin.org/author/jnlin/</author_url><title>避免 PHP Imagick 把記憶體吃完...</title><html>PHP 的 Imagick 有 memory leak 的情形，當用 PHP-CGI 跑動態縮圖服務的時候，這個問題更加的嚴重。&lt;a href=&quot;stackoverflow.com/questions/9993085/php-imagick-memory-leak&quot;&gt;查了一下資料&lt;/a&gt;，可以設定參數限制使用的記憶體大小：

&lt;code&gt;
// 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);
&lt;/code&gt;

單位是 Bytes。如果進行圖片處理時，所需的記憶體超過指定值時，會在 /tmp 寫入暫存檔案。可以使用 MAGICK_TMPDIR 這個環境變數來指定暫存目錄：

&lt;code&gt;
putenv(&#039;MAGICK_TMPDIR=/tmp/imagick&#039;);
&lt;/code&gt;</html><type>rich</type></oembed>