内存泄漏分析工具的尴尬

February 28, 2008 – 10:15 am

Ruby 内置了 ObjectSpace 可以用来分析当前生存的对象,但是这样的方法有时候并不好用,而且使用 ObjectSpace 会影响到它自己。Evan Weaver 做的 BleakHouse 工具则用 C 直接分析 Heap (以前的版本也是使用 ObjectSpace),可以得到更精确的结果,可以用于内存泄漏的检测。

不过它的文档比较简略, Rails 好像可以直接用,而非 Rails 程序则只要“构建一个 BleakHouse::Logger 对象,并在合适的时候调用其 snapshot 方法”。我不是很清楚什么是“合适的时候”,我尝试在每一轮调用主要执行代码的时候调用一次 snapshot ,结果 dump 出来一个 300+MB 的数据文件。

按照文档的说法,接下来要用 bleak 工具进行分析,并且要 “Be patient; it‘s quite slow.” 结果证明它确实很慢,而且占用内存飙升。我原本给虚拟机分配的是 512MB 的内存,跑一会儿就不行了。我又改成 1G+ 的内存,Host 系统差不多不能做事情了,就让它跑,结果还是很慢,并且不久内存和 swap 空间差不多都要用完了:

memory_leak.png

后来我出门了,不知它跑了多久,我回来的时候结果总算是出来了。不过大概是我 snapshot 的位置不够吧,没有得到太多有用的信息,这实在是非常尴尬的局面。文档上说作者在分析 Rails 程序的时候经常 dump 出 3GB 的数据,如果按照这个比例(300MB 数据需要 1GB 的内存来分析)的话,那我岂不是要 10GB 的内存来跑这个分析程序?看样子在分析我自己那数百 KB 的内存泄漏之前得先处理好这个内存泄漏分析的程序自己的内存占用问题了。 -.-bb

  1. 4 Responses to “内存泄漏分析工具的尴尬”

  2. Yeah, I agree that it’s very problematic. What I have to do is use a parallel memory space as a scratchpad and record the actual line of code at which each reference gets created, and then I can correlate that with the heap references that remain at the end of the profiling. That will eliminate trying to guess where to put snapshot() calls.

    The analysis task’s high memory usage is more an artifact of Ruby not handling huge hashes well. I might need to rewrite the task in OCaml or C.

    By Evan on Feb 29, 2008

  3. @Evan:
    Hi! First I still have to thank you for this great tool. And I’m looking forward for your new faster/lighter version. :) Maybe the analysis can be an increment process — not the whole part be loaded into memory, I can kill it when memory usage is too high and still resume analysising correctly later.

    By pluskid on Feb 29, 2008

  4. gem install ruby-prof

    By Jack on Mar 1, 2008

  5. @Jack:
    I’ve tried ruby-prof. It can show me where I’ve allocated how many memory — even in a cool gui graphic call tree (kcachegrind). But what I want to do is to check where the memory leaked. Maybe this isn’t a precise problem since in a GC language the GC can wake up at any time. :(

    By pluskid on Mar 1, 2008

Post a Comment