[SOLVED] ImageMagick Convert command high memory usage issue

Everybody using ImageMagick’s convert command would be facing this issue. And this could be breaking your head also forcing you to choose other options.

I am using ImageMagick to extract thumbnails of pdf documents. This script is running as a cron job & it is called almost every minute.

Now you can imagine that I would have gone crazy becoz of it. Yes, my memory usage is always 100%, diallowing me to do anything else in the server.

I am using a VPS linux server with a domain hosted. The domain will be accessed by public users. Now, since this cron job is using 100% memory, the site never responds. It takes about 1 min to load each page.

I started thinking for other options and decided to use ghostscript gs command. But this was not delivering output in the same quality as convert command. So I had to go back to convert command and find a solution to memory issue.

-limit command option was like a good solution for me. Refer: http://www.imagemagick.org/script/command-line-options.php#limit. So the command was like

convert -limit area 1GiB -limit disk 5EiB -limit memory 1GiB -limit map 2GiB -density 500 "' . $fullpath . '" -resample ' . $dpi . ' "' . $destFolder . '/%d.jpg"'

I looked at the process memory usage using top command in console.


But it was the same. It occupied full memory. The site was not responding.

Finally, I found this link http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=10847

I used the below command

nice -n 19 convert -density 500 "' . $fullpath . '" -resample ' . $dpi . ' "' . $destFolder . '/%d.jpg"'

Note that using ‘nice’ will not prevent a command from using 100% CPU. What it does do is say that if any other process needs CPU, it will get a higher priority than the ‘nice’ processes. At nice 19, that means the niced process will generally not run unless no other process needs the CPU, that it it uses ‘idle’ time.

Now the memory usage is still shown as 99.9% for convert command. But my site is responsive. It responds in a second, like normal.

I am now fine with convert command using 99.9%, as long as the site is responsive and end users don’t find a difference.

Hope this is helpful for others too… !!!

Share This

Leave a Reply

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

+ four = 8