Perl 6でのメモリーリークのチェック方法

こんにちは、Perl 6アドベントカレンダーの18日目の投稿になります。

今回は簡単なTipsとして、Perl 6で書いたプログラムのメモリーリークのチェック方法を紹介しようと思います。
perl6のインストール時に同梱されている`perl6-valgrind-m`を使うと簡単にチェックできます。

# -eオプションで実行

+ その場で一行のプログラムを書いて実行することができます

```bash:-eオプション
$ perl6-valgrind-m -e '"hello".say'
```

```bash:実行結果
================================================================================================
This is Rakudo Perl 6 running in valgrind, a tool for debugging and profiling programs.
Running a program in valgrind usually takes *a lot* more time than running it directly,
so please be patient.
This Rakudo version is 2016.11.182.gea.28845 built on MoarVM version 2016.11.41.gd.2139.b.5,
running on debian (8.jessie) / linux (3.16.0.4.amd.64)
------------------------------------------------------------------------------------------------
==7083== Memcheck, a memory error detector
==7083== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7083== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==7083== Command: /home/itoyota/.rakudobrew/moar-nom/install/bin/moar --full-cleanup --execname=/home/itoyota/.rakudobrew/bin/../moar-nom/install/bin/perl6-valgrind-m --libpath=/home/itoyota/.rakudobrew/moar-nom/install/share/nqp/lib --libpath=/home/itoyota/.rakudobrew/moar-nom/install/share/perl6/lib --libpath=/home/itoyota/.rakudobrew/moar-nom/install/share/perl6/runtime /home/itoyota/.rakudobrew/moar-nom/install/share/perl6/runtime/perl6.moarvm -e "hello".say
==7083== 
hello
==7083== 
==7083== HEAP SUMMARY:
==7083==     in use at exit: 259,267 bytes in 39,001 blocks
==7083==   total heap usage: 244,187 allocs, 205,186 frees, 55,380,461 bytes allocated
==7083== 
==7083== LEAK SUMMARY:
==7083==    definitely lost: 8,766 bytes in 34,573 blocks
==7083==    indirectly lost: 22,260 bytes in 668 blocks
==7083==      possibly lost: 210,056 bytes in 3,751 blocks
==7083==    still reachable: 18,185 bytes in 9 blocks
==7083==         suppressed: 0 bytes in 0 blocks
==7083== Rerun with --leak-check=full to see details of leaked memory
==7083== 
==7083== For counts of detected and suppressed errors, rerun with: -v
==7083== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

```

# テストに対して実行

+ テストプログラムに対して実行するのがよくあるユースケースかなと思います

```bash:実行
$ perl6-valgrind-m t/01-tagger.t
```

```bash:実行結果
================================================================================================
This is Rakudo Perl 6 running in valgrind, a tool for debugging and profiling programs.
Running a program in valgrind usually takes *a lot* more time than running it directly,
so please be patient.
This Rakudo version is 2016.11.182.gea.28845 built on MoarVM version 2016.11.41.gd.2139.b.5,
running on debian (8.jessie) / linux (3.16.0.4.amd.64)
------------------------------------------------------------------------------------------------
==7107== Memcheck, a memory error detector
==7107== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7107== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==7107== Command: /home/itoyota/.rakudobrew/moar-nom/install/bin/moar --full-cleanup --execname=/home/itoyota/.rakudobrew/bin/../moar-nom/install/bin/perl6-valgrind-m --libpath=/home/itoyota/.rakudobrew/moar-nom/install/share/nqp/lib --libpath=/home/itoyota/.rakudobrew/moar-nom/install/share/perl6/lib --libpath=/home/itoyota/.rakudobrew/moar-nom/install/share/perl6/runtime /home/itoyota/.rakudobrew/moar-nom/install/share/perl6/runtime/perl6.moarvm t/01-tagger.t
==7107== 
ok 1 - 
ok 2 - 
    ok 1 - 
    ok 2 - 
    1..2
ok 3 - MeCab::Tagger.parse-tonode should return a fulfilled MeCab::Node object.
1..3
==7107== 
==7107== HEAP SUMMARY:
==7107==     in use at exit: 704,650 bytes in 43,912 blocks
==7107==   total heap usage: 361,886 allocs, 317,974 frees, 110,964,543 bytes allocated
==7107== 
==7107== LEAK SUMMARY:
==7107==    definitely lost: 9,511 bytes in 39,336 blocks
==7107==    indirectly lost: 325,824 bytes in 794 blocks
==7107==      possibly lost: 341,152 bytes in 3,755 blocks
==7107==    still reachable: 28,163 bytes in 27 blocks
==7107==         suppressed: 0 bytes in 0 blocks
==7107== Rerun with --leak-check=full to see details of leaked memory
==7107== 
==7107== For counts of detected and suppressed errors, rerun with: -v
==7107== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
```

以上、Perl 6アドベントカレンダーの18日目の投稿でした。


**P.S.**
今回紹介したコマンドはMoarVMかコンパイラのメモリリークを検知するので、
なにかメモリーリークを実際に見つけてしまったら、[既に報告がないか確認](http://qiita.com/titsuki/items/549ac95b8c37feb1dee1#perl6fail)したうえで[RTする](http://qiita.com/titsuki/items/a5aa81a67f3fe2dc5fc5#rt)とよいと思います。

参考(公式ドキュメントになかったのでISSUE立ててみました)：https://github.com/perl6/doc/issues/1060