Limit space and memory used by Imagemagick

2019-09-02 17:36发布

问题:

I'm used Imagemagick on a rails application (with rmagick) but my server (Ubuntu) it's not very big and when i launch my convert process Imagemagick take all place/memory of my server (30GB HDD).

I would like limit the memory and the tmp file size, how can i do it ?

回答1:

Try running the command

identify -list resource

to see the resources you can control/limit. It gives this:

Resource limits:
  Width: 214.7MP
  Height: 214.7MP
  Area: 4.295GP
  Memory: 2GiB       <--- Default is 2 GiB
  Map: 4GiB
  Disk: unlimited
  File: 1920
  Thread: 1
  Throttle: 0
  Time: unlimited

Then you can control the memory using commands like this for both convert and identify and mogrify

identify -limit memory 1GiB -list resource
Resource limits:
  Width: 214.7MP
  Height: 214.7MP
  Area: 4.295GP
  Memory: 1GiB         <--- 1 GiB for duration of this command
  Map: 4GiB
  Disk: unlimited
  File: 1920
  Thread: 1
  Throttle: 0
  Time: unlimited

So, as you can see above, I limited memory to 1GB for the one command.

Equally, you can limit memory for convert with

convert -limit memory 1GiB -size ... file.png

You can also set the memory limit in an environment variable, single-shot, like this:

MAGICK_MEMORY_LIMIT=30000 identify -list resource
Resource limits:
  Width: 214.7MP
  Height: 214.7MP
  Area: 4.295GP
  Memory: 29.3KiB
  Map: 4GiB
  Disk: unlimited
  File: 1920
  Thread: 1
  Throttle: 0
  Time: unlimited

or like this for the rest of the session:

export MAGICK_MEMORY_LIMIT=500000
identify -list resource

Resource limits:
  Width: 214.7MP
  Height: 214.7MP
  Area: 4.295GP
  Memory: 488KiB
  Map: 4GiB
  Disk: unlimited
  File: 1920
  Thread: 1
  Throttle: 0
  Time: unlimited

Kurt also offers some great advice here.



回答2:

$ man ulimit

Read about the -f entry and do:

$ ulimit -f 1024000
$ convert blah blah blah
$ ulimit -f <usual-value>

should limit the convert process to writing a 1G file. Be sure to restore the original value afterward.