title: How do I use Valgrind with Ruby?
body: <p>I've compiled ruby (<code>2.2.4</code>) with <code>debugflags='-g' optflags='-O0' ./configure --with-valgrind</code> as suggested by the <a href="http://valgrind.org/docs/manual/quick-start.html" rel="noreferrer">valgrind-docs</a>. According to at least several though <a href="https://blog.evanweaver.com/2008/02/05/valgrind-and-ruby/" rel="noreferrer">obscure resources</a> people have used Valgrind with ruby successfully, more so the <code>--with-valgrind</code> option appears to exist as of <code>1.9.3</code></p>
  
  <blockquote>
    <p>./doc/ChangeLog-1.9.3:62800:  * configure.in: add --with-valgrind.</p>
  </blockquote>
  
  <p>The error I have is identical to the one referred to in the <a href="https://stackoverflow.com/questions/20112989/how-to-use-valgrind-with-python">analogous python question</a>, where even a basic test program reports numerous false positives for 'reference lost' / 'probably lost'.</p>
  
  <p>sample output:</p>
  
  <p><code>valgrind --tool=memcheck --leak-check=yes --max-stackframe=8382448 --track-origins=yes ruby test.rb</code></p>
  
  <pre><code>==15105== HEAP SUMMARY:
  ==15105==     in use at exit: 840,786 bytes in 8,050 blocks
  ==15105==   total heap usage: 10,496 allocs, 2,446 frees, 2,373,732 bytes allocated
  ==15105== 
  ==15105== 16 bytes in 1 blocks are possibly lost in loss record 87 of 5,245
  ==15105==    at 0x4C2ABD0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==15105==    by 0x1511F4: objspace_xmalloc0 (gc.c:7780)
  ==15105==    by 0x1512CB: objspace_xmalloc (gc.c:7792)
  ==15105==    by 0x15153F: ruby_xmalloc (gc.c:7871)
  ==15105==    by 0x2E54CA: rb_class_subclass_add (class.c:41)
  ==15105==    by 0x2E546F: RCLASS_SET_SUPER (internal.h:602)
  ==15105==    by 0x2E58E8: rb_class_boot (class.c:205)
  ==15105==    by 0x2E687B: boot_defclass (class.c:537)
  ==15105==    by 0x2E6937: Init_class_hierarchy (class.c:554)
  ==15105==    by 0x193EEE: InitVM_Object (object.c:3371)
  ==15105==    by 0x1951C8: Init_Object (object.c:3596)
  ==15105==    by 0x15C07F: rb_call_inits (inits.c:23)
  .....
  </code></pre>