Convert uses too much memory

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tfe
Posts: 3
Joined: 2013-07-17T02:29:35-07:00
Authentication code: 6789

Convert uses too much memory

Post by tfe » 2013-07-17T02:49:33-07:00

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.

User avatar
magick
Site Admin
Posts: 10831
Joined: 2003-05-31T11:32:55-07:00

Re: Convert uses too much memory

Post by magick » 2013-07-17T04:17:41-07:00

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.

tfe
Posts: 3
Joined: 2013-07-17T02:29:35-07:00
Authentication code: 6789

Re: Convert uses too much memory

Post by tfe » 2013-07-17T04:59:14-07:00

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#

tfe
Posts: 3
Joined: 2013-07-17T02:29:35-07:00
Authentication code: 6789

Re: Convert uses too much memory

Post by tfe » 2013-07-18T04:06:30-07:00

magick, thanks. Changed the policy file. Hope it will help.

User avatar
anthony
Posts: 8869
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Convert uses too much memory

Post by anthony » 2013-07-28T21:37:54-07:00

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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

Post Reply