Page 1 of 1

Convert uses too much memory

Posted: 2013-07-17T02:49:33-07:00
by tfe
I have 3 servers. All of them Debian + nginx+ php5-fpm + imagemagick. I installed Imagemagick using the commant apt-get install imagemagick.

All 3 servers have the same problem. From time to time I get emails from all servers with warnings about excessive resource usage.

Code: Select all

Time:         Tue Jul 16 17:02:18 2013 +0000
Account:      www-data
Resource:     Virtual Memory Size
Exceeded:     386 > 200 (MB)
Executable:   /usr/bin/convert
Command Line: convert -geometry 800x532 /home/site2/pics/16745fgtr543hdt.jpg /home/site2/pics/16745fgtr543hdt.jpg
PID:          2302 (Parent PID:2301)
Killed:       No
My users upload hundreds of jpg images every day and the maximum allowed file size is 4 MB. Warning emails I receive maybe2-10 times per day but I think it's not normal that Imagemagick sometimes needs 386 MB and more memory to resize one jpg image < 4 MB.

What do you suggest to limit memory usage? What is the correct way to do this? Like this:

Code: Select all

exec("convert -geometry -limit memory 32MiB -limit map 64MiB  \"".$width1."x".$height1."\" \"$upfile\" \"$upfile\""); 
Is this correct? Should I leave 32 MiB and 64 Mib as it is?

Thanks.

Re: Convert uses too much memory

Posted: 2013-07-17T04:17:41-07:00
by magick
You can set system-wide limits with the policy.xml configuration file. Ours looks like this:

Code: Select all

<policymap>
  <policy domain="resource" name="temporary-path" value="/tmp"/>
  <policy domain="resource" name="memory" value="256MiB"/>
  <policy domain="resource" name="map" value="512MiB"/>
  <policy domain="resource" name="area" value="128MB"/>
  <policy domain="resource" name="disk" value="1GiB"/>
  <policy domain="resource" name="file" value="768"/>
  <policy domain="resource" name="thread" value="2"/>
  <policy domain="resource" name="throttle" value="0"/>
  <policy domain="resource" name="time" value="120"/>
  <policy domain="system" name="precision" value="6"/>
  <policy domain="cache" name="shared-secret" value="********"/>
</policymap>
Adjust the arguments to fit your environment.

Re: Convert uses too much memory

Posted: 2013-07-17T04:59:14-07:00
by tfe
something strange... server can not locate imagemagick...

Code: Select all

serv5:/usr/bin# which imagemagick
serv5:/usr/bin# whereis imagemagick
imagemagick:
serv5:/usr/bin#
serv5:/usr/bin# apt-get install imagemagick
Reading package lists... Done
Building dependency tree
Reading state information... Done
imagemagick is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
serv5:/usr/bin#
serv5:/usr/bin# which convert
/usr/bin/convert
serv5:/usr/bin#

Re: Convert uses too much memory

Posted: 2013-07-18T04:06:30-07:00
by tfe
magick, thanks. Changed the policy file. Hope it will help.

Re: Convert uses too much memory

Posted: 2013-07-28T21:37:54-07:00
by anthony
For JPEG to make a image that is just less than 4MB it actually has to save that image multiple times at different quality settings until it finds on that is just under that limit.

See IM Examples, Common File Formats, JPEG write settings, "jpeg:extent"
http://www.imagemagick.org/Usage/formats/#jpg_write

THAT is very slow!

Better method. Just check input image size, if the image is too big.. warn user!
But also Use a fixed compression quality, and limit pixel size for input image.

A short trial with very complex test images should give you a rough idea what image size limits should be.

Users understand pixel size limits... they really do not understand compression limits.