12

I often use ImageMagick's convert for *->PNG conversion, but when PDF has more than 50 pages — convert eats more that 3 Gib (!!!) of memory. I guess it first loads everything.

That's unacceptable. It should read PDF page by page, why the heck all of them at once!

Maybe there's a way to tune it somehow? Or any good alternatives?

kolypto
  • 10,738
  • 12
  • 51
  • 66

3 Answers3

11

I am using the following:

convert -limit memory 64 -limit map 128 original.djvu newfile.pdf

My main drive has limited space, so I prepend a variable

env MAGICK_TMPDIR=/host/Temp convert -limit memory 64 -limit map 128 original.djvu newfile.pdf
dufte
  • 97
  • 4
  • Using the `-limit` seemed to push disk usage when I used it. It seems odd that imagemagick is using as much memory and disk as it is... since surely one can render just one page at a time... but I guess there might be some parallelism going on. – Att Righ Jun 24 '18 at 12:42
11

Solved with the following:

cat <<EOF > /etc/profile.d/ImageMagick.sh
# Set ImageMagick memory limits: it eats too much
export MAGICK_MEMORY_LIMIT=1024 # Use up to *MB of memory before doing mmap
export MAGICK_MAP_LIMIT=1024    # Use up to *MB mmaps before caching to disk
export MAGICK_AREA_LIMIT=4096   # Use up to *MB disk space before failure
export MAGICK_FILES_LIMIT=1024  # Don't open more than *file handles
EOF
kolypto
  • 10,738
  • 12
  • 51
  • 66
9

Have you tried cache?

From the man page

-cache threshold

      megabytes of memory available to the pixel cache.

      Image pixels are stored in memory until 80 megabytes of
      memory have been consumed.  Subsequent pixel operations

      are cached on disk.  Operations to memory are  significantly 
      faster but if your computer does not have a sufficient 
      amount of free memory you may  want  to  adjust
      this threshold value.
Shikoru
  • 101
  • 1
  • 3
  • 1
    Is it an option for `convert`? I have only "-limit type value pixel cache resource limit". Plus your quote says "80Mb is the default", but my convert eats all RAM :) – kolypto Dec 25 '09 at 18:30
  • 80Mb may be the default if you just add the -cache without a value following it. – Shikoru Dec 25 '09 at 18:33
  • 1
    Hmm, my IMagick has only "-limit memory 64" to limit its memory to 64MB. It works, thanks anyway! :) – kolypto Dec 25 '09 at 18:45
  • 2
    Found: " -cache (This option has been replaced by the -limit option)" – kolypto Dec 25 '09 at 18:48
  • 1
    @kolypto In my interpretation, ImageMagick websites says that the default unit is bytes: "The value for file is in number of files. The other limits are in bytes. Define arguments for the memory, map, area, and disk resource limits with SI prefixes (.e.g 100MB)." http://www.imagemagick.org/script/command-line-options.php#limit – thomasa88 Apr 28 '13 at 11:02