getalog

console.log geta6

OSXでコマンドラインからメモリ使用量を調査する

vm_statを使う、osxにはfreeやら/procといった便利な概念が存在しないので。

osx(10.8)でvm_statを実行するとこのような出力が得られる。

$ vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                         232518.
Pages active:                       802343.
Pages inactive:                      73295.
Pages speculative:                  584616.
Pages wired down:                   403993.
"Translation faults":            175092932.
Pages copy-on-write:               6189507.
Pages zero filled:               204021301.
Pages reactivated:                   73037.
Pageins:                           3797404.
Pageouts:                              280.
Object cache: 19 hits of 724452 lookups (0% hit rate)

pagesizeが4096なので、例えば空きメモリなら232518*4096バイト、と解釈する。

で、整形しろと言わんばかりの出力形式だったのでnode.jsで整形してみた。

exec = (require 'child_process').exec

exec 'vm_stat', (e, stdout, stderr) ->
  stats = {}
  for stat in stdout.split '\n'
    stat = stat.match /^(?:Pages )*(.+?):+?.*?([0-9]+).*$/
    if stat?
      if stat[1] is 'Mach Virtual Memory Statistics'
        ps = parseInt stat[2], 10
      else unless stat[1].match /[- "]/
        stats[stat[1]] = stat[2] * ps
  console.log stats

結果はこうなります。

{ free: 879509504,
  active: 3349544960,
  inactive: 319320064,
  speculative: 2389131264,
  reactivated: 299159552,
  Pageins: 15625510912,
  Pageouts: 1146880 }

mac mini発売とサーバリプレイスを控えてosx用にチューニングしたmunin的監視ツールが欲しかったので、その一環。