Skip to content

Instantly share code, notes, and snippets.

@zunda
Last active November 26, 2017 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunda/ad0f7eed03b7ebbee7a365fd74870118 to your computer and use it in GitHub Desktop.
Save zunda/ad0f7eed03b7ebbee7a365fd74870118 to your computer and use it in GitHub Desktop.
valgrindでImageMagickのメモリの挙動を確認する

http://techlife.cookpad.com/entry/2016/05/18/180703 を参考に。

$ cp /usr/share/doc/nodejs/guides/doc_img/compare-boxplot.png /tmp/t.png
$ convert -debug Resource t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png
  :
2017-11-25T11:39:12-10:00 0:00.550 0.910u 6.9.7 Resource convert-im6.q16[7236]: resource.c/RelinquishMagickResource/1013/Resource
  Memory: 13.11MB/0B/256MiB

デバグの表示はよくわからない。valgrindを使ってみよう。

デフォルトではこの場合最大66.9MiB使用

$ valgrind --tool=massif convert t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png
$ ls -tr massif.out.* | tail -1 | xargs ms_print | less
  :
 Detailed snapshots: [20, 22, 24, 25, 30, 41, 45 (peak), 55, 65, 75]
  :
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  :
 45    624,292,496       70,129,928       70,111,869        18,059            0
  :

MAGICK_AREA_LIMITを128KiBにすると最大736.7KiB使用

$ MAGICK_AREA_LIMIT=131072 valgrind --tool=massif convert t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png
$ ls -tr massif.out.* | tail -1 | xargs ms_print | less
  :
 Detailed snapshots: [20, 25, 31, 37, 42, 44, 58, 65, 68, 78, 88 (peak)]
  :
 88  4,219,812,388          754,368          738,081        16,287            0

MAGICK_MEMORY_LIMITを128KiBにすると最大736.6KiB使用

$ MAGICK_MEMORY_LIMIT=131072 valgrind --tool=massif convert t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png
$ ls -tr massif.out.* | tail -1 | xargs ms_print | less
  :
 94  4,222,065,192          754,256          738,082        16,174            0
  :

MAGICK_AREA_LIMITMAGICK_MEMORY_LIMITを128KiBにすると最大736.6KiB使用

$ MAGICK_AREA_LIMIT=131072 MAGICK_MEMORY_LIMIT=131072 valgrind --tool=massif convert t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png
$ ls -tr massif.out.* | tail -1 | xargs ms_print | less
  :
 86  4,221,203,438          754,256          738,082        16,174            0
  :

環境変数で挙動が変化することは確かめられたがリミットよりも多く使っているのはなぜだろう。

$ MAGICK_MEMORY_LIMIT=131072 convert -debug Resource t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png

なんやわからないねえ。

MAGICK_MEMORY_LIMITを512MiBにする

512MBの実環境に合わせてみる。67MiB使用。画像が小さすぎるか。

$ MAGICK_MEMORY_LIMIT=536870912 valgrind --tool=massif convert t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png
$ ls -tr massif.out.* | tail -1 | xargs ms_print | less
  :
 45    624,308,653       70,129,816       70,111,869        17,947            0
  :

32MiBだと13MiB使用。リミットをMiB単位に大きくしておけば期待する挙動になるのかな?

$ MAGICK_MEMORY_LIMIT=33554432 valgrind --tool=massif convert t.png -auto-orient -resize "1280x1280>" -quality 90 -strip u.png
$ ls -tr massif.out.* | tail -1 | xargs ms_print | less
  :
 89  4,221,767,229       13,861,352       13,845,221        16,131            0
  :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment