Skip to content

Instantly share code, notes, and snippets.

@crakaC
Last active March 10, 2018 04:19
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 crakaC/2a65489adbbc7ec24ec7bb6b1dadd91a to your computer and use it in GitHub Desktop.
Save crakaC/2a65489adbbc7ec24ec7bb6b1dadd91a to your computer and use it in GitHub Desktop.
ImageMagickでgifをconvertする時のメモリ消費を何とかしたい

Mastodonでgifアイコンをconvertするときにメモリを激しく消費する。

120x120、1フレームのgifアイコンをconvertするときのメモリ消費

$ identify 1frame.gif
#> 1frame.gif GIF 120x120 120x120+0+0 8-bit sRGB 256c 13041B 0.000u 0:00.000
--------------------------------------------------------------------------------
Command:            convert -coalesce -auto-orient -resize 120x -crop 120x120+0+0 +repage -layers optimize -quality 80 -strip 1frame.gif out.gif
Massif arguments:   (none)
ms_print arguments: massif.out.12017
--------------------------------------------------------------------------------


    MB
128.8^                                                         :
     |                                           :::::::::::::#::::::::::::::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
     |                                           :            #:           ::
   0 +----------------------------------------------------------------------->Mi
     0                                                                   73.42

〜中略〜
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 40     46,906,638      134,951,144      134,919,211        31,933            0
 41     60,039,960      134,951,352      134,919,363        31,989            0
99.98% (134,919,363B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->99.50% (134,279,680B) 0x4F69C33: AcquireAlignedMemory (memory.c:262)
| ->99.46% (134,217,728B) 0x4F6A05B: AcquireVirtualMemory (memory.c:635)
| | ->99.46% (134,217,728B) 0x4F9C703: GetCubeInfo (quantize.c:2038)
| |   ->99.46% (134,217,728B) 0x4F9C954: QuantizeImage (quantize.c:2699)
| |     ->99.46% (134,217,728B) 0x558157A: MogrifyImageList (mogrify.c:8408)
| |       ->99.46% (134,217,728B) 0x5582074: MogrifyImages (mogrify.c:8952)
| |         ->99.46% (134,217,728B) 0x550B5FA: ConvertImageCommand (convert.c:3273)
| |           ->99.46% (134,217,728B) 0x5576B8A: MagickCommandGenesis (mogrify.c:183)
| |             ->99.46% (134,217,728B) 0x4011F9: MagickMain (magick.c:149)
| |               ->99.46% (134,217,728B) 0x581F82E: (below main) (libc-start.c:291)

何が起きているんだ🤔

media_attachmentに同じ画像を投げたときのメモリ消費

--------------------------------------------------------------------------------
Command:            ffmpeg -i 1frame.gif -c:a none -strict experimental -movflags faststart -pix_fmt yuv420p -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -vsync cfr -b:v 1300K -maxrate 500K -bufsize 1300K -crf 18 -y out.mp4
Massif arguments:   --massif-out-file=gifv.out
ms_print arguments: gifv.out
--------------------------------------------------------------------------------


    MB
7.944^                                                                       #
     |                                                                  :::::#
     |                                                                  ::   #
     |                                                                  ::   #
     |                                                                  ::   #
     |                                                                  ::   #
     |                                                                  ::   #
     |                                                                  ::   #
     |                                                                 :::   #
     |                                                                ::::   #
     |                                                               :::::   #
     |                                                              ::::::   #
     |                                                             :::::::   #
     |                                                             :::::::   #
     |                                                            ::::::::   #
     |                                                           :::::::::   #
     |                                                          ::::::::::   #
     |                                                         :::::::::::   #
     |                                                        ::::::::::::   #
     |                                                     :::::::::::::::   #
   0 +----------------------------------------------------------------------->Mi
     0                                                                   111.8
@crakaC
Copy link
Author

crakaC commented Dec 8, 2017

GIF allocates 128MB of memory to dither - ImageMagick
CacheShiftを3に固定してみたらメモリ消費が激減した。
ImageMagick/quantize.c at master · ImageMagick/ImageMagick
CacheShiftの値は、macOS, iOS上だと3で、その他の環境だと2になるようだ。

サーバーと、手元のMacで全然メモリ消費が違ったのはこのせいらしい。

@crakaC
Copy link
Author

crakaC commented Dec 8, 2017

CacheShift=3にしてビルドした結果。

--------------------------------------------------------------------------------
Command:            convert -coalesce -auto-orient -resize 120x -crop 120x120+0+0 +repage -quality 80 -layers optimize -strip 1frame.gif out.gif
Massif arguments:   (none)
ms_print arguments: massif.out.23872
--------------------------------------------------------------------------------


    MB
8.767^                                                     :
     |                                     :::::::::::::::#:::::::::::::::::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |                                     :              #::             ::
     |            @ :@::@@:@:@::  ::::::::@:              #::             ::
   0 +----------------------------------------------------------------------->Mi
     0                                                                   59.07

やったぜ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment