我发现我的Rails代码中存在内存泄漏 - 也就是说,我发现了代码泄漏但不泄漏的原因.我把它减少到不需要Rails的测试用例:
require 'csspool'
require 'ruby-mass'
def report
puts 'Memory ' + `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_s + 'KB'
Mass.print
end
report
# note I do not store the return value here
CSSPool::CSS::Document.parse(File.new('/home/jason/big.css'))
ObjectSpace.garbage_collect
sleep 1
report
Run Code Online (Sandbox Code Playgroud)
据说红宝石质量可以让我看到记忆中的所有物体.CSSPool是一个基于racc的CSS解析器./home/jason/big.css是一个1.5MB的CSS文件.
这输出:
Memory 9264KB
==================================================
Objects within [] namespace
==================================================
String: 7261
RubyVM::InstructionSequence: 1151
Array: 562
Class: 313
Regexp: 181
Proc: 111
Encoding: 99
Gem::StubSpecification: 66
Gem::StubSpecification::StubLine: …Run Code Online (Sandbox Code Playgroud)